
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>10.1 安装项目 · 《CMake Cookbook》中文版</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="陈晓伟 chenxiaowei">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="10.2-chinese.html" />
    
    
    <link rel="prev" href="10.0-chinese.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../preface/preface-chinese.html">
            
                <a href="../preface/preface-chinese.html">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter0/0.0-chinese.html">
            
                <a href="../chapter0/0.0-chinese.html">
            
                    
                    第0章 配置环境
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter0/0.1-chinese.html">
            
                <a href="../chapter0/0.1-chinese.html">
            
                    
                    0.1 获取代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter0/0.2-chinese.html">
            
                <a href="../chapter0/0.2-chinese.html">
            
                    
                    0.2 Docker镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter0/0.3-chinese.html">
            
                <a href="../chapter0/0.3-chinese.html">
            
                    
                    0.3 安装必要的软件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter0/0.4-chinese.html">
            
                <a href="../chapter0/0.4-chinese.html">
            
                    
                    0.4 测试环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter0/0.5-chinese.html">
            
                <a href="../chapter0/0.5-chinese.html">
            
                    
                    0.5 上报问题并提出改进建议
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter1/1.0-chinese.html">
            
                <a href="../chapter1/1.0-chinese.html">
            
                    
                    第1章 从可执行文件到库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter1/1.1-chinese.html">
            
                <a href="../chapter1/1.1-chinese.html">
            
                    
                    1.1 将单个源文件编译为可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter1/1.2-chinese.html">
            
                <a href="../chapter1/1.2-chinese.html">
            
                    
                    1.2 切换生成器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter1/1.3-chinese.html">
            
                <a href="../chapter1/1.3-chinese.html">
            
                    
                    1.3 构建和链接静态库和动态库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter1/1.4-chinese.html">
            
                <a href="../chapter1/1.4-chinese.html">
            
                    
                    1.4 用条件句控制编译
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../chapter1/1.5-chinese.html">
            
                <a href="../chapter1/1.5-chinese.html">
            
                    
                    1.5 向用户显示选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../chapter1/1.6-chinese.html">
            
                <a href="../chapter1/1.6-chinese.html">
            
                    
                    1.6 指定编译器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../chapter1/1.7-chinese.html">
            
                <a href="../chapter1/1.7-chinese.html">
            
                    
                    1.7 切换构建类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../chapter1/1.8-chinese.html">
            
                <a href="../chapter1/1.8-chinese.html">
            
                    
                    1.8 设置编译器选项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="../chapter1/1.9-chinese.html">
            
                <a href="../chapter1/1.9-chinese.html">
            
                    
                    1.9 为语言设定标准
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="../chapter1/1.10-chinese.html">
            
                <a href="../chapter1/1.10-chinese.html">
            
                    
                    1.10 使用控制流
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter2/2.0-chinese.html">
            
                <a href="../chapter2/2.0-chinese.html">
            
                    
                    第2章 检测环境
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter2/2.1-chinese.html">
            
                <a href="../chapter2/2.1-chinese.html">
            
                    
                    2.1 检测操作系统
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter2/2.2-chinese.html">
            
                <a href="../chapter2/2.2-chinese.html">
            
                    
                    2.2 处理与平台相关的源代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../chapter2/2.3-chinese.html">
            
                <a href="../chapter2/2.3-chinese.html">
            
                    
                    2.3 处理与编译器相关的源代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../chapter2/2.4-chinese.html">
            
                <a href="../chapter2/2.4-chinese.html">
            
                    
                    2.4 检测处理器体系结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../chapter2/2.5-chinese.html">
            
                <a href="../chapter2/2.5-chinese.html">
            
                    
                    2.5 检测处理器指令集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../chapter2/2.6-chinese.html">
            
                <a href="../chapter2/2.6-chinese.html">
            
                    
                    2.6 为Eigen库使能向量化
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter3/3.0-chinese.html">
            
                <a href="../chapter3/3.0-chinese.html">
            
                    
                    第3章 检测外部库和程序
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter3/3.1-chinese.html">
            
                <a href="../chapter3/3.1-chinese.html">
            
                    
                    3.1 检测Python解释器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter3/3.2-chinese.html">
            
                <a href="../chapter3/3.2-chinese.html">
            
                    
                    3.2 检测Python库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../chapter3/3.3-chinese.html">
            
                <a href="../chapter3/3.3-chinese.html">
            
                    
                    3.3 检测Python模块和包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../chapter3/3.4-chinese.html">
            
                <a href="../chapter3/3.4-chinese.html">
            
                    
                    3.4 检测BLAS和LAPACK数学库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../chapter3/3.5-chinese.html">
            
                <a href="../chapter3/3.5-chinese.html">
            
                    
                    3.5 检测OpenMP的并行环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../chapter3/3.6-chinese.html">
            
                <a href="../chapter3/3.6-chinese.html">
            
                    
                    3.6 检测MPI的并行环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../chapter3/3.7-chinese.html">
            
                <a href="../chapter3/3.7-chinese.html">
            
                    
                    3.7 检测Eigen库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../chapter3/3.8-chinese.html">
            
                <a href="../chapter3/3.8-chinese.html">
            
                    
                    3.8 检测Boost库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../chapter3/3.9-chinese.html">
            
                <a href="../chapter3/3.9-chinese.html">
            
                    
                    3.9 检测外部库:Ⅰ. 使用pkg-config
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="../chapter3/3.10-chinese.html">
            
                <a href="../chapter3/3.10-chinese.html">
            
                    
                    3.10 检测外部库:Ⅱ. 自定义find模块
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter4/4.0-chinese.html">
            
                <a href="../chapter4/4.0-chinese.html">
            
                    
                    第4章 创建和运行测试
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter4/4.1-chinese.html">
            
                <a href="../chapter4/4.1-chinese.html">
            
                    
                    4.1 创建一个简单的单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter4/4.2-chinese.html">
            
                <a href="../chapter4/4.2-chinese.html">
            
                    
                    4.2 使用Catch2库进行单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter4/4.3-chinese.html">
            
                <a href="../chapter4/4.3-chinese.html">
            
                    
                    4.3 使用Google Test库进行单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter4/4.4-chinese.html">
            
                <a href="../chapter4/4.4-chinese.html">
            
                    
                    4.4 使用Boost Test进行单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../chapter4/4.5-chinese.html">
            
                <a href="../chapter4/4.5-chinese.html">
            
                    
                    4.5 使用动态分析来检测内存缺陷
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../chapter4/4.6-chinese.html">
            
                <a href="../chapter4/4.6-chinese.html">
            
                    
                    4.6 预期测试失败
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="../chapter4/4.7-chinese.html">
            
                <a href="../chapter4/4.7-chinese.html">
            
                    
                    4.7 使用超时测试运行时间过长的测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.8" data-path="../chapter4/4.8-chinese.html">
            
                <a href="../chapter4/4.8-chinese.html">
            
                    
                    4.8 并行测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.9" data-path="../chapter4/4.9-chinese.html">
            
                <a href="../chapter4/4.9-chinese.html">
            
                    
                    4.9 运行测试子集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.10" data-path="../chapter4/4.10-chinese.html">
            
                <a href="../chapter4/4.10-chinese.html">
            
                    
                    4.10 使用测试固件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter5/5.0-chinese.html">
            
                <a href="../chapter5/5.0-chinese.html">
            
                    
                    第5章 配置时和构建时的操作
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter5/5.1-chinese.html">
            
                <a href="../chapter5/5.1-chinese.html">
            
                    
                    5.1 使用平台无关的文件操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter5/5.2-chinese.html">
            
                <a href="../chapter5/5.2-chinese.html">
            
                    
                    5.2 配置时运行自定义命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter5/5.3-chinese.html">
            
                <a href="../chapter5/5.3-chinese.html">
            
                    
                    5.3 构建时运行自定义命令:Ⅰ. 使用add_custom_command
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../chapter5/5.4-chinese.html">
            
                <a href="../chapter5/5.4-chinese.html">
            
                    
                    5.4 构建时运行自定义命令:Ⅱ. 使用add_custom_target
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../chapter5/5.5-chinese.html">
            
                <a href="../chapter5/5.5-chinese.html">
            
                    
                    5.5 构建时为特定目标运行自定义命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.6" data-path="../chapter5/5.6-chinese.html">
            
                <a href="../chapter5/5.6-chinese.html">
            
                    
                    5.6 探究编译和链接命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.7" data-path="../chapter5/5.7-chinese.html">
            
                <a href="../chapter5/5.7-chinese.html">
            
                    
                    5.7 探究编译器标志命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.8" data-path="../chapter5/5.8-chinese.html">
            
                <a href="../chapter5/5.8-chinese.html">
            
                    
                    5.8 探究可执行命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.9" data-path="../chapter5/5.9-chinese.html">
            
                <a href="../chapter5/5.9-chinese.html">
            
                    
                    5.9 使用生成器表达式微调配置和编译
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter6/6.0-chinese.html">
            
                <a href="../chapter6/6.0-chinese.html">
            
                    
                    第6章 生成源码
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter6/6.1-chinese.html">
            
                <a href="../chapter6/6.1-chinese.html">
            
                    
                    6.1 配置时生成源码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter6/6.2-chinese.html">
            
                <a href="../chapter6/6.2-chinese.html">
            
                    
                    6.2 使用Python在配置时生成源码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter6/6.3-chinese.html">
            
                <a href="../chapter6/6.3-chinese.html">
            
                    
                    6.3 构建时使用Python生成源码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../chapter6/6.4-chinese.html">
            
                <a href="../chapter6/6.4-chinese.html">
            
                    
                    6.4 记录项目版本信息以便报告
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="../chapter6/6.5-chinese.html">
            
                <a href="../chapter6/6.5-chinese.html">
            
                    
                    6.5 从文件中记录项目版本
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.6" data-path="../chapter6/6.6-chinese.html">
            
                <a href="../chapter6/6.6-chinese.html">
            
                    
                    6.6 配置时记录Git Hash值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.7" data-path="../chapter6/6.7-chinese.html">
            
                <a href="../chapter6/6.7-chinese.html">
            
                    
                    6.7 构建时记录Git Hash值
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../chapter7/7.0-chinese.html">
            
                <a href="../chapter7/7.0-chinese.html">
            
                    
                    第7章 构建项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../chapter7/7.1-chinese.html">
            
                <a href="../chapter7/7.1-chinese.html">
            
                    
                    7.1 使用函数和宏重用代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../chapter7/7.2-chinese.html">
            
                <a href="../chapter7/7.2-chinese.html">
            
                    
                    7.2 将CMake源代码分成模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../chapter7/7.3-chinese.html">
            
                <a href="../chapter7/7.3-chinese.html">
            
                    
                    7.3 编写函数来测试和设置编译器标志
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="../chapter7/7.4-chinese.html">
            
                <a href="../chapter7/7.4-chinese.html">
            
                    
                    7.4 用指定参数定义函数或宏
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="../chapter7/7.5-chinese.html">
            
                <a href="../chapter7/7.5-chinese.html">
            
                    
                    7.5 重新定义函数和宏
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.6" data-path="../chapter7/7.6-chinese.html">
            
                <a href="../chapter7/7.6-chinese.html">
            
                    
                    7.6 使用废弃函数、宏和变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.7" data-path="../chapter7/7.7-chinese.html">
            
                <a href="../chapter7/7.7-chinese.html">
            
                    
                    7.7 add_subdirectory的限定范围
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.8" data-path="../chapter7/7.8-chinese.html">
            
                <a href="../chapter7/7.8-chinese.html">
            
                    
                    7.8 使用target_sources避免全局变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.9" data-path="../chapter7/7.9-chinese.html">
            
                <a href="../chapter7/7.9-chinese.html">
            
                    
                    7.9 组织Fortran项目
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../chapter8/8.0-chinese.html">
            
                <a href="../chapter8/8.0-chinese.html">
            
                    
                    第8章 超级构建模式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../chapter8/8.1-chinese.html">
            
                <a href="../chapter8/8.1-chinese.html">
            
                    
                    8.1 使用超级构建模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../chapter8/8.2-chinese.html">
            
                <a href="../chapter8/8.2-chinese.html">
            
                    
                    8.2 使用超级构建管理依赖项:Ⅰ.Boost库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="../chapter8/8.3-chinese.html">
            
                <a href="../chapter8/8.3-chinese.html">
            
                    
                    8.3 使用超级构建管理依赖项:Ⅱ.FFTW库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.4" data-path="../chapter8/8.4-chinese.html">
            
                <a href="../chapter8/8.4-chinese.html">
            
                    
                    8.4 使用超级构建管理依赖项:Ⅲ.Google Test框架
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.5" data-path="../chapter8/8.5-chinese.html">
            
                <a href="../chapter8/8.5-chinese.html">
            
                    
                    8.5 使用超级构建支持项目
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../chapter9/9.0-chinese.html">
            
                <a href="../chapter9/9.0-chinese.html">
            
                    
                    第9章 语言混合项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.12.1" data-path="../chapter9/9.1-chinese.html">
            
                <a href="../chapter9/9.1-chinese.html">
            
                    
                    9.1 使用C/C++库构建Fortran项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.2" data-path="../chapter9/9.2-chinese.html">
            
                <a href="../chapter9/9.2-chinese.html">
            
                    
                    9.2 使用Fortran库构建C/C++项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.3" data-path="../chapter9/9.3-chinese.html">
            
                <a href="../chapter9/9.3-chinese.html">
            
                    
                    9.3 使用Cython构建C++和Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.4" data-path="../chapter9/9.4-chinese.html">
            
                <a href="../chapter9/9.4-chinese.html">
            
                    
                    9.4 使用Boost.Python构建C++和Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.5" data-path="../chapter9/9.5-chinese.html">
            
                <a href="../chapter9/9.5-chinese.html">
            
                    
                    9.5 使用pybind11构建C++和Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.6" data-path="../chapter9/9.6-chinese.html">
            
                <a href="../chapter9/9.6-chinese.html">
            
                    
                    9.6 使用Python CFFI混合C，C++，Fortran和Python
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="10.0-chinese.html">
            
                <a href="10.0-chinese.html">
            
                    
                    第10章 编写安装程序
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.13.1" data-path="10.1-chinese.html">
            
                <a href="10.1-chinese.html">
            
                    
                    10.1 安装项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.2" data-path="10.2-chinese.html">
            
                <a href="10.2-chinese.html">
            
                    
                    10.2 生成输出头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.3" data-path="10.3-chinese.html">
            
                <a href="10.3-chinese.html">
            
                    
                    10.3 输出目标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.4" data-path="10.4-chinese.html">
            
                <a href="10.4-chinese.html">
            
                    
                    10.4 安装超级构建
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../chapter11/11.0-chinese.html">
            
                <a href="../chapter11/11.0-chinese.html">
            
                    
                    第11章 打包项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../chapter11/11.1-chinese.html">
            
                <a href="../chapter11/11.1-chinese.html">
            
                    
                    11.1 生成源代码和二进制包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../chapter11/11.2-chinese.html">
            
                <a href="../chapter11/11.2-chinese.html">
            
                    
                    11.2 通过PyPI发布使用CMake/pybind11构建的C++/Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.3" data-path="../chapter11/11.3-chinese.html">
            
                <a href="../chapter11/11.3-chinese.html">
            
                    
                    11.3 通过PyPI发布使用CMake/CFFI构建C/Fortran/Python项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.4" data-path="../chapter11/11.4-chinese.html">
            
                <a href="../chapter11/11.4-chinese.html">
            
                    
                    11.4 以Conda包的形式发布一个简单的项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.5" data-path="../chapter11/11.5-chinese.html">
            
                <a href="../chapter11/11.5-chinese.html">
            
                    
                    11.5 将Conda包作为依赖项发布给项目
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../chapter12/12.0-chinese.html">
            
                <a href="../chapter12/12.0-chinese.html">
            
                    
                    第12章 构建文档
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.15.1" data-path="../chapter12/12.1-chinese.html">
            
                <a href="../chapter12/12.1-chinese.html">
            
                    
                    12.1 使用Doxygen构建文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.2" data-path="../chapter12/12.2-chinese.html">
            
                <a href="../chapter12/12.2-chinese.html">
            
                    
                    12.2 使用Sphinx构建文档
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.3" data-path="../chapter12/12.3-chinese.html">
            
                <a href="../chapter12/12.3-chinese.html">
            
                    
                    12.3 结合Doxygen和Sphinx
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../chapter13/13.0-chinese.html">
            
                <a href="../chapter13/13.0-chinese.html">
            
                    
                    第13章 选择生成器和交叉编译
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.16.1" data-path="../chapter13/13.1-chinese.html">
            
                <a href="../chapter13/13.1-chinese.html">
            
                    
                    13.1 使用CMake构建Visual Studio 2017项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.2" data-path="../chapter13/13.2-chinese.html">
            
                <a href="../chapter13/13.2-chinese.html">
            
                    
                    13.2 交叉编译hello world示例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.3" data-path="../chapter13/13.3-chinese.html">
            
                <a href="../chapter13/13.3-chinese.html">
            
                    
                    13.3 使用OpenMP并行化交叉编译Windows二进制文件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../chapter14/14.0-chinese.html">
            
                <a href="../chapter14/14.0-chinese.html">
            
                    
                    第14章 测试面板
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.17.1" data-path="../chapter14/14.1-chinese.html">
            
                <a href="../chapter14/14.1-chinese.html">
            
                    
                    14.1 将测试部署到CDash
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.2" data-path="../chapter14/14.2-chinese.html">
            
                <a href="../chapter14/14.2-chinese.html">
            
                    
                    14.2 CDash显示测试覆盖率
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.3" data-path="../chapter14/14.3-chinese.html">
            
                <a href="../chapter14/14.3-chinese.html">
            
                    
                    14.3 使用AddressSanifier向CDash报告内存缺陷
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.4" data-path="../chapter14/14.4-chinese.html">
            
                <a href="../chapter14/14.4-chinese.html">
            
                    
                    14.4 使用ThreadSaniiser向CDash报告数据争用
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../chapter15/15.0-chinese.html">
            
                <a href="../chapter15/15.0-chinese.html">
            
                    
                    第15章 使用CMake构建已有项目
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.18.1" data-path="../chapter15/15.1-chinese.html">
            
                <a href="../chapter15/15.1-chinese.html">
            
                    
                    15.1 如何开始迁移项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.2" data-path="../chapter15/15.2-chinese.html">
            
                <a href="../chapter15/15.2-chinese.html">
            
                    
                    15.2 生成文件并编写平台检查
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.3" data-path="../chapter15/15.3-chinese.html">
            
                <a href="../chapter15/15.3-chinese.html">
            
                    
                    15.3 检测所需的链接和依赖关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.4" data-path="../chapter15/15.4-chinese.html">
            
                <a href="../chapter15/15.4-chinese.html">
            
                    
                    15.4 复制编译标志
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.5" data-path="../chapter15/15.5-chinese.html">
            
                <a href="../chapter15/15.5-chinese.html">
            
                    
                    15.5 移植测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.6" data-path="../chapter15/15.6-chinese.html">
            
                <a href="../chapter15/15.6-chinese.html">
            
                    
                    15.6 移植安装目标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.7" data-path="../chapter15/15.7-chinese.html">
            
                <a href="../chapter15/15.7-chinese.html">
            
                    
                    15.7 进一步迁移的措施
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.8" data-path="../chapter15/15.8-chinese.html">
            
                <a href="../chapter15/15.8-chinese.html">
            
                    
                    15.8 项目转换为CMake的常见问题
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../chapter16/16.0-chinese.html">
            
                <a href="../chapter16/16.0-chinese.html">
            
                    
                    第16章 可能感兴趣的书
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.19.1" data-path="../chapter16/16.1-chinese.html">
            
                <a href="../chapter16/16.1-chinese.html">
            
                    
                    16.1 留下评论——让其他读者知道你的想法
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="http://www.bczl.xyz/" target="blank" class="gitbook-link">
            编程之路
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >10.1 安装项目</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="101-&#x5B89;&#x88C5;&#x9879;&#x76EE;">10.1 &#x5B89;&#x88C5;&#x9879;&#x76EE;</h1>
<p><strong>NOTE</strong>:<em>&#x6B64;&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#x53EF;&#x4EE5;&#x5728; <a href="https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-10/recipe-01" target="_blank">https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-10/recipe-01</a> &#x4E2D;&#x627E;&#x5230;&#xFF0C;&#x5176;&#x4E2D;&#x6709;&#x4E00;&#x4E2A;C++&#x793A;&#x4F8B;&#x548C;&#x4E00;&#x4E2A;Fortran&#x793A;&#x4F8B;&#x3002;&#x8BE5;&#x793A;&#x4F8B;&#x5728;CMake 3.6&#x7248;(&#x6216;&#x66F4;&#x9AD8;&#x7248;&#x672C;)&#x4E2D;&#x662F;&#x6709;&#x6548;&#x7684;&#xFF0C;&#x5E76;&#x4E14;&#x5DF2;&#x7ECF;&#x5728;GNU/Linux&#x3001;macOS&#x548C;Windows&#x4E0A;&#x8FDB;&#x884C;&#x8FC7;&#x6D4B;&#x8BD5;&#x3002;</em></p>
<p>&#x7B2C;&#x4E00;&#x4E2A;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;&#x5C06;&#x4ECB;&#x7ECD;&#x6211;&#x4EEC;&#x7684;&#x5C0F;&#x9879;&#x76EE;&#x548C;&#x4E00;&#x4E9B;&#x57FA;&#x672C;&#x6982;&#x5FF5;&#xFF0C;&#x8FD9;&#x4E9B;&#x6982;&#x5FF5;&#x4E5F;&#x5C06;&#x5728;&#x540E;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#x4F7F;&#x7528;&#x3002;&#x5B89;&#x88C5;&#x6587;&#x4EF6;&#x3001;&#x5E93;&#x548C;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x662F;&#x4E00;&#x9879;&#x975E;&#x5E38;&#x57FA;&#x7840;&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x4F46;&#x662F;&#x4E5F;&#x53EF;&#x80FD;&#x4F1A;&#x5E26;&#x6765;&#x4E00;&#x4E9B;&#x7F3A;&#x9677;&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x5E26;&#x60A8;&#x4E86;&#x89E3;&#x8FD9;&#x4E9B;&#x95EE;&#x9898;&#xFF0C;&#x5E76;&#x5C55;&#x793A;&#x5982;&#x4F55;&#x4F7F;&#x7528;CMake&#x6709;&#x6548;&#x5730;&#x907F;&#x5F00;&#x8FD9;&#x4E9B;&#x7F3A;&#x9677;&#x3002;</p>
<h2 id="&#x51C6;&#x5907;&#x5DE5;&#x4F5C;">&#x51C6;&#x5907;&#x5DE5;&#x4F5C;</h2>
<p>&#x7B2C;1&#x7AE0;&#x7B2C;3&#x8282;&#x7684;&#x793A;&#x4F8B;&#xFF0C;&#x51E0;&#x4E4E;&#x590D;&#x7528;&#xFF1A;&#x53EA;&#x6DFB;&#x52A0;&#x5BF9;UUID&#x5E93;&#x7684;&#x4F9D;&#x8D56;&#x3002;&#x8FD9;&#x4E2A;&#x4F9D;&#x8D56;&#x662F;&#x6709;&#x6761;&#x4EF6;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x627E;&#x5230;UUID&#x5E93;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x901A;&#x8FC7;&#x9884;&#x5904;&#x7406;&#x7A0B;&#x5E8F;&#x6392;&#x9664;&#x4F7F;&#x7528;UUID&#x5E93;&#x7684;&#x4EE3;&#x7801;&#x3002;&#x9879;&#x76EE;&#x5E03;&#x5C40;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-shell">.
&#x251C;&#x2500;&#x2500; CMakeLists.txt
&#x251C;&#x2500;&#x2500; src
&#x2502;    &#x251C;&#x2500;&#x2500; CMakeLists.txt
&#x2502;    &#x251C;&#x2500;&#x2500; hello-world.cpp
&#x2502;    &#x251C;&#x2500;&#x2500; Message.cpp
&#x2502;    &#x2514;&#x2500;&#x2500; Message.hpp
&#x2514;&#x2500;&#x2500; tests
    &#x2514;&#x2500;&#x2500; CMakeLists.txt
</code></pre>
<p>&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x770B;&#x5230;&#xFF0C;&#x6709;&#x4E09;&#x4E2A;<code>CMakeLists.txt</code>&#xFF0C;&#x4E00;&#x4E2A;&#x662F;&#x4E3B;<code>CMakeLists.txt</code>&#xFF0C;&#x53E6;&#x4E00;&#x4E2A;&#x662F;&#x4F4D;&#x4E8E;<code>src</code>&#x76EE;&#x5F55;&#x4E0B;&#x7684;&#xFF0C;&#x8FD8;&#x6709;&#x4E00;&#x4E2A;&#x662F;&#x4F4D;&#x4E8E;<code>test</code>&#x76EE;&#x5F55;&#x4E0B;&#x7684;&#x3002;</p>
<p><code>Message.hpp</code>&#x5934;&#x6587;&#x4EF6;&#x5305;&#x542B;&#x4EE5;&#x4E0B;&#x5185;&#x5BB9;:</p>
<pre><code class="lang-c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">pragma</span> once</span>

<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iosfwd&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string&gt;</span></span>

<span class="hljs-keyword">class</span> Message
{
<span class="hljs-keyword">public</span>:
  Message(<span class="hljs-keyword">const</span> <span class="hljs-built_in">std</span>::<span class="hljs-built_in">string</span> &amp;m) : message_(m) {}
  <span class="hljs-keyword">friend</span> <span class="hljs-built_in">std</span>::ostream &amp;<span class="hljs-keyword">operator</span>&lt;&lt;(<span class="hljs-built_in">std</span>::ostream &amp;os, Message &amp;obj)
  {
    <span class="hljs-keyword">return</span> obj.printObject(os);
  }

<span class="hljs-keyword">private</span>:
  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">string</span> message_;
  <span class="hljs-built_in">std</span>::<span class="hljs-function">ostream &amp;<span class="hljs-title">printObject</span><span class="hljs-params">(<span class="hljs-built_in">std</span>::ostream &amp;os)</span></span>;
};

<span class="hljs-built_in">std</span>::<span class="hljs-function"><span class="hljs-built_in">string</span> <span class="hljs-title">getUUID</span><span class="hljs-params">()</span></span>;
</code></pre>
<p><code>Message.cpp</code>&#x4E2D;&#x6709;&#x76F8;&#x5E94;&#x7684;&#x5B9E;&#x73B0;&#xFF1A;</p>
<pre><code class="lang-c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-string">&quot;Message.hpp&quot;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">ifdef</span> HAVE_UUID</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;uuid/uuid.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>
<span class="hljs-built_in">std</span>::ostream &amp;Message::printObject(<span class="hljs-built_in">std</span>::ostream &amp;os)
{
  os &lt;&lt; <span class="hljs-string">&quot;This is my very nice message: &quot;</span> &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
  os &lt;&lt; message_ &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
  os &lt;&lt; <span class="hljs-string">&quot;...and here is its UUID: &quot;</span> &lt;&lt; getUUID();
  <span class="hljs-keyword">return</span> os;
}
<span class="hljs-meta">#<span class="hljs-meta-keyword">ifdef</span> HAVE_UUID</span>
<span class="hljs-built_in">std</span>::<span class="hljs-function"><span class="hljs-built_in">string</span> <span class="hljs-title">getUUID</span><span class="hljs-params">()</span>
</span>{
  <span class="hljs-keyword">uuid_t</span> uuid;
  uuid_generate(uuid);
  <span class="hljs-keyword">char</span> uuid_str[<span class="hljs-number">37</span>];
  uuid_unparse_lower(uuid, uuid_str);
  uuid_clear(uuid);
  <span class="hljs-built_in">std</span>::<span class="hljs-function"><span class="hljs-built_in">string</span> <span class="hljs-title">uuid_cxx</span><span class="hljs-params">(uuid_str)</span></span>;
  <span class="hljs-keyword">return</span> uuid_cxx;
}
<span class="hljs-meta">#<span class="hljs-meta-keyword">else</span></span>
<span class="hljs-built_in">std</span>::<span class="hljs-function"><span class="hljs-built_in">string</span> <span class="hljs-title">getUUID</span><span class="hljs-params">()</span>
</span>{
  <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Ooooops, no UUID for you!&quot;</span>;
}
<span class="hljs-meta">#<span class="hljs-meta-keyword">endif</span></span>
</code></pre>
<p>&#x6700;&#x540E;&#xFF0C;&#x793A;&#x4F8B;<code>hello-world.cpp</code>&#x5185;&#x5BB9;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-c++"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;cstdlib&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;iostream&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-string">&quot;Message.hpp&quot;</span></span>
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
  <span class="hljs-function">Message <span class="hljs-title">say_hello</span><span class="hljs-params">(<span class="hljs-string">&quot;Hello, CMake World!&quot;</span>)</span></span>;
  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; say_hello &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
  <span class="hljs-function">Message <span class="hljs-title">say_goodbye</span><span class="hljs-params">(<span class="hljs-string">&quot;Goodbye, CMake World&quot;</span>)</span></span>;
  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; say_goodbye &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
  <span class="hljs-keyword">return</span> EXIT_SUCCESS;
}
</code></pre>
<h2 id="&#x5177;&#x4F53;&#x5B9E;&#x65BD;">&#x5177;&#x4F53;&#x5B9E;&#x65BD;</h2>
<p>&#x6211;&#x4EEC;&#x5148;&#x6765;&#x770B;&#x4E00;&#x4E0B;&#x4E3B;<code>CMakeLists.txt</code>:</p>
<ol>
<li><p>&#x58F0;&#x660E;CMake&#x6700;&#x4F4E;&#x7248;&#x672C;&#xFF0C;&#x5E76;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;C++11&#x9879;&#x76EE;&#x3002;&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x4E3A;&#x6211;&#x4EEC;&#x7684;&#x9879;&#x76EE;&#x8BBE;&#x7F6E;&#x4E86;&#x4E00;&#x4E2A;&#x7248;&#x672C;&#xFF0C;&#x5728;<code>project</code>&#x4E2D;&#x4F7F;&#x7528;<code>VERSION</code>&#x8FDB;&#x884C;&#x6307;&#x5B9A;:</p>
<pre><code class="lang-cmake"><span class="hljs-comment"># CMake 3.6 needed for IMPORTED_TARGET option</span>
<span class="hljs-comment"># to pkg_search_module</span>
<span class="hljs-keyword">cmake_minimum_required</span>(VERSION <span class="hljs-number">3.6</span> FATAL_ERROR)
<span class="hljs-keyword">project</span>(recipe-<span class="hljs-number">01</span>
LANGUAGES CXX
VERSION <span class="hljs-number">1.0</span>.<span class="hljs-number">0</span>
)
<span class="hljs-comment"># &lt;&lt;&lt; General set up &gt;&gt;&gt;</span>
<span class="hljs-keyword">set</span>(CMAKE_CXX_STANDARD <span class="hljs-number">11</span>)
<span class="hljs-keyword">set</span>(CMAKE_CXX_EXTENSIONS <span class="hljs-keyword">OFF</span>)
<span class="hljs-keyword">set</span>(CMAKE_CXX_STANDARD_REQUIRED <span class="hljs-keyword">ON</span>)
</code></pre>
</li>
<li><p>&#x7528;&#x6237;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>CMAKE_INSTALL_PREFIX</code>&#x53D8;&#x91CF;&#x5B9A;&#x4E49;&#x5B89;&#x88C5;&#x76EE;&#x5F55;&#x3002;CMake&#x4F1A;&#x7ED9;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x9ED8;&#x8BA4;&#x503C;&#xFF1A;Windows&#x4E0A;&#x7684;<code>C:\Program Files</code>&#x548C;Unix&#x4E0A;&#x7684;<code>/usr/local</code>&#x3002;&#x6211;&#x4EEC;&#x5C06;&#x4F1A;&#x6253;&#x5370;&#x5B89;&#x88C5;&#x76EE;&#x5F55;&#x7684;&#x4FE1;&#x606F;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;Project will be installed to ${CMAKE_INSTALL_PREFIX}&quot;</span>)
</code></pre>
</li>
<li><p>&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x66F4;&#x559C;&#x6B22;&#x4EE5;Release&#x7684;&#x65B9;&#x5F0F;&#x914D;&#x7F6E;&#x9879;&#x76EE;&#x3002;&#x7528;&#x6237;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;<code>CMAKE_BUILD_TYPE</code>&#x8BBE;&#x7F6E;&#x6B64;&#x53D8;&#x91CF;&#xFF0C;&#x4ECE;&#x800C;&#x6539;&#x53D8;&#x914D;&#x7F6E;&#x7C7B;&#x578B;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x68C0;&#x67E5;&#x662F;&#x5426;&#x5B58;&#x5728;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x3002;&#x5982;&#x679C;&#x6CA1;&#x6709;&#xFF0C;&#x5C06;&#x8BBE;&#x7F6E;&#x4E3A;&#x9ED8;&#x8BA4;&#x503C;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">if</span>(NOT CMAKE_BUILD_TYPE)
    <span class="hljs-keyword">set</span>(CMAKE_BUILD_TYPE Release CACHE <span class="hljs-keyword">STRING</span> <span class="hljs-string">&quot;Build type&quot;</span> FORCE)
<span class="hljs-keyword">endif</span>()
<span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;Build type set to ${CMAKE_BUILD_TYPE}&quot;</span>)
</code></pre>
</li>
<li><p>&#x63A5;&#x4E0B;&#x6765;&#xFF0C;&#x544A;&#x8BC9;CMake&#x5728;&#x4F55;&#x5904;&#x6784;&#x5EFA;&#x53EF;&#x6267;&#x884C;&#x3001;&#x9759;&#x6001;&#x548C;&#x52A8;&#x6001;&#x5E93;&#x76EE;&#x6807;&#x3002;&#x4FBF;&#x4E8E;&#x5728;&#x7528;&#x6237;&#x4E0D;&#x6253;&#x7B97;&#x5B89;&#x88C5;&#x9879;&#x76EE;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x8BBF;&#x95EE;&#x8FD9;&#x4E9B;&#x6784;&#x5EFA;&#x76EE;&#x6807;&#x3002;&#x8FD9;&#x91CC;&#x4F7F;&#x7528;&#x6807;&#x51C6;CMake&#x7684;<code>GNUInstallDirs.cmake</code>&#x6A21;&#x5757;&#x3002;&#x8FD9;&#x5C06;&#x786E;&#x4FDD;&#x7684;&#x9879;&#x76EE;&#x5E03;&#x5C40;&#x7684;&#x5408;&#x7406;&#x6027;&#x548C;&#x53EF;&#x79FB;&#x690D;&#x6027;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">include</span>(GNUInstallDirs)

<span class="hljs-keyword">set</span>(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
    <span class="hljs-variable">${PROJECT_BINARY_DIR}</span>/<span class="hljs-variable">${CMAKE_INSTALL_LIBDIR}</span>)
<span class="hljs-keyword">set</span>(CMAKE_LIBRARY_OUTPUT_DIRECTORY
    <span class="hljs-variable">${PROJECT_BINARY_DIR}</span>/<span class="hljs-variable">${CMAKE_INSTALL_LIBDIR}</span>)
<span class="hljs-keyword">set</span>(CMAKE_RUNTIME_OUTPUT_DIRECTORY
    <span class="hljs-variable">${PROJECT_BINARY_DIR}</span>/<span class="hljs-variable">${CMAKE_INSTALL_BINDIR}</span>)
</code></pre>
</li>
<li><p>&#x867D;&#x7136;&#xFF0C;&#x524D;&#x9762;&#x7684;&#x547D;&#x4EE4;&#x914D;&#x7F6E;&#x4E86;&#x6784;&#x5EFA;&#x76EE;&#x5F55;&#x4E2D;&#x8F93;&#x51FA;&#x7684;&#x4F4D;&#x7F6E;&#xFF0C;&#x4F46;&#x662F;&#x9700;&#x8981;&#x4E0B;&#x9762;&#x7684;&#x547D;&#x4EE4;&#x6765;&#x914D;&#x7F6E;&#x53EF;&#x6267;&#x884C;&#x7A0B;&#x5E8F;&#x3001;&#x5E93;&#x4EE5;&#x53CA;&#x5B89;&#x88C5;&#x524D;&#x7F00;&#x4E2D;&#x5305;&#x542B;&#x7684;&#x6587;&#x4EF6;&#x7684;&#x4F4D;&#x7F6E;&#x3002;&#x5B83;&#x4EEC;&#x5927;&#x81F4;&#x9075;&#x5FAA;&#x76F8;&#x540C;&#x7684;&#x5E03;&#x5C40;&#xFF0C;&#x4F46;&#x662F;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E86;&#x65B0;&#x7684;<code>INSTALL_LIBDIR</code>&#x3001;<code>INSTALL_BINDIR</code>&#x3001;<code>INSTALL_INCLUDEDIR</code>&#x548C;<code>INSTALL_CMAKEDIR</code>&#x53D8;&#x91CF;&#x3002;&#x5F53;&#x7136;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x8986;&#x76D6;&#x8FD9;&#x4E9B;&#x53D8;&#x91CF;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-comment"># Offer the user the choice of overriding the installation directories</span>
<span class="hljs-keyword">set</span>(INSTALL_LIBDIR <span class="hljs-variable">${CMAKE_INSTALL_LIBDIR}</span> CACHE PATH <span class="hljs-string">&quot;Installation directory for libraries&quot;</span>)
<span class="hljs-keyword">set</span>(INSTALL_BINDIR <span class="hljs-variable">${CMAKE_INSTALL_BINDIR}</span> CACHE PATH <span class="hljs-string">&quot;Installation directory for executables&quot;</span>)
<span class="hljs-keyword">set</span>(INSTALL_INCLUDEDIR <span class="hljs-variable">${CMAKE_INSTALL_INCLUDEDIR}</span> CACHE PATH <span class="hljs-string">&quot;Installation directory for header files&quot;</span>)
<span class="hljs-keyword">if</span>(WIN32 <span class="hljs-keyword">AND</span> NOT CYGWIN)
    <span class="hljs-keyword">set</span>(DEF_INSTALL_CMAKEDIR CMake)
<span class="hljs-keyword">else</span>()
    <span class="hljs-keyword">set</span>(DEF_INSTALL_CMAKEDIR share/cmake/<span class="hljs-variable">${PROJECT_NAME}</span>)
<span class="hljs-keyword">endif</span>()
<span class="hljs-keyword">set</span>(INSTALL_CMAKEDIR <span class="hljs-variable">${DEF_INSTALL_CMAKEDIR}</span> CACHE PATH <span class="hljs-string">&quot;Installation directory for CMake files&quot;</span>)
</code></pre>
</li>
<li><p>&#x62A5;&#x544A;&#x7EC4;&#x4EF6;&#x5B89;&#x88C5;&#x7684;&#x8DEF;&#x5F84;:</p>
<pre><code class="lang-cmake"><span class="hljs-comment"># Report to user</span>
<span class="hljs-keyword">foreach</span>(p LIB BIN <span class="hljs-keyword">INCLUDE</span> CMAKE)
  file(TO_NATIVE_PATH <span class="hljs-variable">${CMAKE_INSTALL_PREFIX}</span>/<span class="hljs-variable">${INSTALL_${p}</span>DIR} _path )
  <span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;Installing ${p} components to ${_path}&quot;</span>)
  <span class="hljs-keyword">unset</span>(_path)
<span class="hljs-keyword">endforeach</span>()
</code></pre>
</li>
<li><p>&#x4E3B;<code>CMakeLists.txt</code>&#x6587;&#x4EF6;&#x4E2D;&#x7684;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x6307;&#x4EE4;&#x6DFB;&#x52A0;<code>src</code>&#x5B50;&#x76EE;&#x5F55;&#xFF0C;&#x542F;&#x7528;&#x6D4B;&#x8BD5;&#xFF0C;&#x5E76;&#x6DFB;&#x52A0;<code>tests</code>&#x5B50;&#x76EE;&#x5F55;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">add_subdirectory</span>(src)
<span class="hljs-keyword">enable_testing</span>()
<span class="hljs-keyword">add_subdirectory</span>(tests)
</code></pre>
</li>
</ol>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x7EE7;&#x7EED;&#x5206;&#x6790;<code>src/CMakeLists.txt</code>&#xFF0C;&#x5176;&#x5B9A;&#x4E49;&#x4E86;&#x6784;&#x5EFA;&#x7684;&#x5B9E;&#x9645;&#x76EE;&#x6807;&#xFF1A;</p>
<ol>
<li><p>&#x6211;&#x4EEC;&#x7684;&#x9879;&#x76EE;&#x4F9D;&#x8D56;&#x4E8E;UUID&#x5E93;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-comment"># Search for pkg-config and UUID</span>
<span class="hljs-keyword">find_package</span>(PkgConfig QUIET)
<span class="hljs-keyword">if</span>(PKG_CONFIG_FOUND)
  pkg_search_module(UUID uuid IMPORTED_TARGET)
  <span class="hljs-keyword">if</span>(TARGET PkgConfig::UUID)
    <span class="hljs-keyword">message</span>(STATUS <span class="hljs-string">&quot;Found libuuid&quot;</span>)
    <span class="hljs-keyword">set</span>(UUID_FOUND <span class="hljs-keyword">TRUE</span>)
  <span class="hljs-keyword">endif</span>()
<span class="hljs-keyword">endif</span>()
</code></pre>
</li>
<li><p>&#x6211;&#x4EEC;&#x5E0C;&#x671B;&#x5EFA;&#x7ACB;&#x4E00;&#x4E2A;&#x52A8;&#x6001;&#x5E93;&#xFF0C;&#x5C06;&#x8BE5;&#x76EE;&#x6807;&#x58F0;&#x660E;&#x4E3A;<code>message-shared</code>:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">add_library</span>(<span class="hljs-keyword">message</span>-shared SHARED <span class="hljs-string">&quot;&quot;</span>)
</code></pre>
</li>
<li><p>&#x8FD9;&#x4E2A;&#x76EE;&#x6807;&#x7531;<code>target_sources</code>&#x547D;&#x4EE4;&#x6307;&#x5B9A;:</p>
<pre><code class="lang-cmake">target_sources(<span class="hljs-keyword">message</span>-shared
  PRIVATE
      <span class="hljs-variable">${CMAKE_CURRENT_LIST_DIR}</span>/<span class="hljs-keyword">Message</span>.cpp
  )
</code></pre>
</li>
<li><p>&#x6211;&#x4EEC;&#x4E3A;&#x76EE;&#x6807;&#x58F0;&#x660E;&#x7F16;&#x8BD1;&#x65F6;&#x5B9A;&#x4E49;&#x548C;&#x94FE;&#x63A5;&#x5E93;&#x3002;&#x8BF7;&#x6CE8;&#x610F;&#xFF0C;&#x6240;&#x6709;&#x8FD9;&#x4E9B;&#x90FD;&#x662F;<code>PUBLIC</code>&#xFF0C;&#x4EE5;&#x786E;&#x4FDD;&#x6240;&#x6709;&#x4F9D;&#x8D56;&#x7684;&#x76EE;&#x6807;&#x5C06;&#x6B63;&#x786E;&#x7EE7;&#x627F;&#x5B83;&#x4EEC;:</p>
<pre><code class="lang-cmake">  target_compile_definitions(<span class="hljs-keyword">message</span>-shared
  PUBLIC
      $&lt;$&lt;BOOL:<span class="hljs-variable">${UUID_FOUND}</span>&gt;:HAVE_UUID&gt;
  )
<span class="hljs-keyword">target_link_libraries</span>(<span class="hljs-keyword">message</span>-shared
  PUBLIC
      $&lt;$&lt;BOOL:<span class="hljs-variable">${UUID_FOUND}</span>&gt;:PkgConfig::UUID&gt;
  )
</code></pre>
</li>
<li><p>&#x7136;&#x540E;&#x8BBE;&#x7F6E;&#x76EE;&#x6807;&#x7684;&#x9644;&#x52A0;&#x5C5E;&#x6027;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">set_target_properties</span>(<span class="hljs-keyword">message</span>-shared
  PROPERTIES
    POSITION_INDEPENDENT_CODE <span class="hljs-number">1</span>
    SOVERSION <span class="hljs-variable">${PROJECT_VERSION_MAJOR}</span>
    OUTPUT_NAME <span class="hljs-string">&quot;message&quot;</span>
    DEBUG_POSTFIX <span class="hljs-string">&quot;_d&quot;</span>
    PUBLIC_HEADER <span class="hljs-string">&quot;Message.hpp&quot;</span>
    MACOSX_RPATH <span class="hljs-keyword">ON</span>
    WINDOWS_EXPORT_ALL_SYMBOLS <span class="hljs-keyword">ON</span>
  )
</code></pre>
</li>
<li><p>&#x6700;&#x540E;&#xFF0C;&#x4E3A;&#x201C;Hello, world&#x201D;&#x7A0B;&#x5E8F;&#x6DFB;&#x52A0;&#x53EF;&#x6267;&#x884C;&#x76EE;&#x6807;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">add_executable</span>(hello-world_wDSO hello-world.cpp)
</code></pre>
</li>
<li><p><code>hello-world_wDSO</code>&#x53EF;&#x6267;&#x884C;&#x76EE;&#x6807;&#xFF0C;&#x4F1A;&#x94FE;&#x63A5;&#x5230;&#x52A8;&#x6001;&#x5E93;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">target_link_libraries</span>(hello-world_wDSO
  PUBLIC
      <span class="hljs-keyword">message</span>-shared
  )
</code></pre>
</li>
</ol>
<p><code>src/CMakeLists.txt</code>&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x8FD8;&#x5305;&#x542B;&#x5B89;&#x88C5;&#x6307;&#x4EE4;&#x3002;&#x8003;&#x8651;&#x8FD9;&#x4E9B;&#x4E4B;&#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x8BBE;&#x7F6E;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x7684;<code>RPATH</code>&#xFF1A;</p>
<ol>
<li><p>&#x4F7F;&#x7528;CMake&#x8DEF;&#x5F84;&#x64CD;&#x4F5C;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;<code>message_RPATH</code>&#x53D8;&#x91CF;&#x3002;&#x8FD9;&#x5C06;&#x4E3A;GNU/Linux&#x548C;macOS&#x8BBE;&#x7F6E;&#x9002;&#x5F53;&#x7684;<code>RPATH</code>:</p>
<pre><code class="lang-cmake">RPATH
file(RELATIVE_PATH _rel <span class="hljs-variable">${CMAKE_INSTALL_PREFIX}</span>/<span class="hljs-variable">${INSTALL_BINDIR}</span> <span class="hljs-variable">${CMAKE_INSTALL_PREFIX}</span>)
<span class="hljs-keyword">if</span>(APPLE)
    <span class="hljs-keyword">set</span>(_rpath <span class="hljs-string">&quot;@loader_path/${_rel}&quot;</span>)
<span class="hljs-keyword">else</span>()
    <span class="hljs-keyword">set</span>(_rpath <span class="hljs-string">&quot;\$ORIGIN/${_rel}&quot;</span>)
<span class="hljs-keyword">endif</span>()
file(TO_NATIVE_PATH <span class="hljs-string">&quot;${_rpath}/${INSTALL_LIBDIR}&quot;</span> message_RPATH)
</code></pre>
</li>
<li><p>&#x73B0;&#x5728;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x6765;&#x8BBE;&#x7F6E;&#x53EF;&#x6267;&#x884C;&#x76EE;&#x6807;<code>hello-world_wDSO</code>&#x7684;<code>RPATH</code>(&#x901A;&#x8FC7;&#x76EE;&#x6807;&#x5C5E;&#x6027;&#x5B9E;&#x73B0;)&#x3002;&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;&#x989D;&#x5916;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x7A0D;&#x540E;&#x4F1A;&#x5BF9;&#x6B64;&#x8FDB;&#x884C;&#x66F4;&#x591A;&#x7684;&#x8BA8;&#x8BBA;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">set_target_properties</span>(hello-world_wDSO
  PROPERTIES
    MACOSX_RPATH <span class="hljs-keyword">ON</span>
    SKIP_BUILD_RPATH <span class="hljs-keyword">OFF</span>
    BUILD_WITH_INSTALL_RPATH <span class="hljs-keyword">OFF</span>
    INSTALL_RPATH <span class="hljs-string">&quot;${message_RPATH}&quot;</span>
    INSTALL_RPATH_USE_LINK_PATH <span class="hljs-keyword">ON</span>
  )
</code></pre>
</li>
<li><p>&#x7EC8;&#x4E8E;&#x53EF;&#x4EE5;&#x5B89;&#x88C5;&#x5E93;&#x3001;&#x5934;&#x6587;&#x4EF6;&#x548C;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x4E86;&#xFF01;&#x4F7F;&#x7528;CMake&#x63D0;&#x4F9B;&#x7684;<code>install</code>&#x547D;&#x4EE4;&#x6765;&#x6307;&#x5B9A;&#x5B89;&#x88C5;&#x4F4D;&#x7F6E;&#x3002;&#x6CE8;&#x610F;&#xFF0C;&#x8DEF;&#x5F84;&#x662F;&#x76F8;&#x5BF9;&#x7684;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x5728;&#x540E;&#x7EED;&#x8FDB;&#x4E00;&#x6B65;&#x8BA8;&#x8BBA;&#x8FD9;&#x4E00;&#x70B9;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">install</span>(
  TARGETS
    <span class="hljs-keyword">message</span>-shared
    hello-world_wDSO
  ARCHIVE
    DESTINATION <span class="hljs-variable">${INSTALL_LIBDIR}</span>
    COMPONENT lib
  RUNTIME
    DESTINATION <span class="hljs-variable">${INSTALL_BINDIR}</span>
    COMPONENT bin
  LIBRARY
    DESTINATION <span class="hljs-variable">${INSTALL_LIBDIR}</span>
    COMPONENT lib
  PUBLIC_HEADER
    DESTINATION <span class="hljs-variable">${INSTALL_INCLUDEDIR}</span>/<span class="hljs-keyword">message</span>
    COMPONENT dev
  )
</code></pre>
</li>
</ol>
<p><code>tests</code>&#x76EE;&#x5F55;&#x4E2D;&#x7684;<code>CMakeLists.txt</code>&#x6587;&#x4EF6;&#x5305;&#x542B;&#x7B80;&#x5355;&#x7684;&#x6307;&#x4EE4;&#xFF0C;&#x4EE5;&#x786E;&#x4FDD;&#x201C;Hello, World&#x201D;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x80FD;&#x591F;&#x6B63;&#x786E;&#x8FD0;&#x884C;&#xFF1A;</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">add_test</span>(
  NAME test_shared
  COMMAND $&lt;TARGET_FILE:hello-world_wDSO&gt;
  )
</code></pre>
<p>&#x73B0;&#x5728;&#x8BA9;&#x6211;&#x4EEC;&#x914D;&#x7F6E;&#x3001;&#x6784;&#x5EFA;&#x548C;&#x5B89;&#x88C5;&#x9879;&#x76EE;&#xFF0C;&#x5E76;&#x67E5;&#x770B;&#x7ED3;&#x679C;&#x3002;&#x6DFB;&#x52A0;&#x5B89;&#x88C5;&#x6307;&#x4EE4;&#x65F6;&#xFF0C;CMake&#x5C31;&#x4F1A;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x540D;&#x4E3A;<code>install</code>&#x7684;&#x65B0;&#x76EE;&#x6807;&#xFF0C;&#x8BE5;&#x76EE;&#x6807;&#x5C06;&#x8FD0;&#x884C;&#x5B89;&#x88C5;&#x89C4;&#x5219;:</p>
<pre><code class="lang-shell">$ mkdir -p build
$ cd build
$ cmake -G&quot;Unix Makefiles&quot; -DCMAKE_INSTALL_PREFIX=$HOME/Software/recipe-01
$ cmake --build . --target install
</code></pre>
<p>GNU/Linux&#x6784;&#x5EFA;&#x76EE;&#x5F55;&#x7684;&#x5185;&#x5BB9;&#x5982;&#x4E0B;:</p>
<pre><code class="lang-shell">build
&#x251C;&#x2500;&#x2500; bin
&#x2502;    &#x2514;&#x2500;&#x2500; hello-world_wDSO
&#x251C;&#x2500;&#x2500; CMakeCache.txt
&#x251C;&#x2500;&#x2500; CMakeFiles
&#x251C;&#x2500;&#x2500; cmake_install.cmake
&#x251C;&#x2500;&#x2500; CTestTestfile.cmake
&#x251C;&#x2500;&#x2500; install_manifest.txt
&#x251C;&#x2500;&#x2500; lib64
&#x2502;    &#x251C;&#x2500;&#x2500; libmessage.so -&gt; libmessage.so.1
&#x2502;    &#x2514;&#x2500;&#x2500; libmessage.so.1
&#x251C;&#x2500;&#x2500; Makefile
&#x251C;&#x2500;&#x2500; src
&#x251C;&#x2500;&#x2500; Testing
&#x2514;&#x2500;&#x2500; tests
</code></pre>
<p>&#x53E6;&#x4E00;&#x65B9;&#x9762;&#xFF0C;&#x5728;&#x5B89;&#x88C5;&#x4F4D;&#x7F6E;&#xFF0C;&#x53EF;&#x4EE5;&#x627E;&#x5230;&#x5982;&#x4E0B;&#x7684;&#x76EE;&#x5F55;&#x7ED3;&#x6784;:</p>
<pre><code class="lang-shell">$HOME/Software/recipe-01/
&#x251C;&#x2500;&#x2500; bin
&#x2502;    &#x2514;&#x2500;&#x2500; hello-world_wDSO
&#x251C;&#x2500;&#x2500; include
&#x2502;    &#x2514;&#x2500;&#x2500; message
&#x2502;        &#x2514;&#x2500;&#x2500; Message.hpp
&#x2514;&#x2500;&#x2500; lib64
    &#x251C;&#x2500;&#x2500; libmessage.so -&gt; libmessage.so.1
    &#x2514;&#x2500;&#x2500; libmessage.so.1
</code></pre>
<p>&#x8FD9;&#x610F;&#x5473;&#x7740;&#x5B89;&#x88C5;&#x6307;&#x4EE4;&#x4E2D;&#x7ED9;&#x51FA;&#x7684;&#x4F4D;&#x7F6E;&#xFF0C;&#x662F;&#x76F8;&#x5BF9;&#x4E8E;&#x7528;&#x6237;&#x7ED9;&#x5B9A;&#x7684;<code>CMAKE_INSTALL_PREFIX</code>&#x8DEF;&#x5F84;&#x3002;</p>
<h2 id="&#x5DE5;&#x4F5C;&#x539F;&#x7406;">&#x5DE5;&#x4F5C;&#x539F;&#x7406;</h2>
<p>&#x8FD9;&#x4E2A;&#x793A;&#x4F8B;&#x6709;&#x4E09;&#x4E2A;&#x8981;&#x70B9;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x66F4;&#x8BE6;&#x7EC6;&#x5730;&#x8BA8;&#x8BBA;&#xFF1A;</p>
<ul>
<li>&#x4F7F;&#x7528;<code>GNUInstallDirs.cmake</code>&#x5B9A;&#x4E49;&#x76EE;&#x6807;&#x5B89;&#x88C5;&#x7684;&#x6807;&#x51C6;&#x4F4D;&#x7F6E;</li>
<li>&#x5728;&#x52A8;&#x6001;&#x5E93;&#x548C;&#x53EF;&#x6267;&#x884C;&#x76EE;&#x6807;&#x4E0A;&#x8BBE;&#x7F6E;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x7279;&#x522B;&#x662F;<code>RPATH</code>&#x7684;&#x5904;&#x7406;</li>
<li>&#x5B89;&#x88C5;&#x6307;&#x4EE4;</li>
</ul>
<h3 id="&#x5B89;&#x88C5;&#x5230;&#x6807;&#x51C6;&#x4F4D;&#x7F6E;">&#x5B89;&#x88C5;&#x5230;&#x6807;&#x51C6;&#x4F4D;&#x7F6E;</h3>
<p>&#x5BF9;&#x4E8E;&#x9879;&#x76EE;&#x7684;&#x5B89;&#x88C5;&#x6765;&#x8BF4;&#xFF0C;&#x4EC0;&#x4E48;&#x662F;&#x597D;&#x7684;&#x5E03;&#x5C40;&#x5462;&#xFF1F;&#x5982;&#x679C;&#x53EA;&#x6709;&#x81EA;&#x5DF1;&#x4F7F;&#x7528;&#x8BE5;&#x9879;&#x76EE;&#xFF0C;&#x90A3;&#x5C31;&#x65E0;&#x6240;&#x8C13;&#x597D;&#x6216;&#x574F;&#x7684;&#x5E03;&#x5C40;&#x3002;&#x7136;&#x800C;&#xFF0C;&#x4E00;&#x65E6;&#x5411;&#x5916;&#x90E8;&#x53D1;&#x5E03;&#x4EA7;&#x54C1;&#xFF0C;&#x548C;&#x4ED6;&#x4EBA;&#x5171;&#x7528;&#x8BE5;&#x9879;&#x76EE;&#xFF0C;&#x5C31;&#x5E94;&#x8BE5;&#x5728;&#x5B89;&#x88C5;&#x9879;&#x76EE;&#x65F6;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x5408;&#x7406;&#x7684;&#x5E03;&#x5C40;&#x3002;&#x5E78;&#x8FD0;&#x7684;&#x662F;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x9075;&#x5FAA;&#x4E00;&#x4E9B;&#x6807;&#x51C6;&#xFF0C;CMake&#x53EF;&#x4EE5;&#x5E2E;&#x52A9;&#x6211;&#x4EEC;&#x505A;&#x5230;&#x8FD9;&#x4E00;&#x70B9;&#x3002;&#x5B9E;&#x9645;&#x4E0A;&#xFF0C;<code>GNUInstallDirs.cmake</code>&#x6A21;&#x5757;&#x6240;&#x505A;&#x7684;&#x5C31;&#x662F;&#x5B9A;&#x4E49;&#x8FD9;&#x6837;&#x4E00;&#x7EC4;&#x53D8;&#x91CF;&#xFF0C;&#x8FD9;&#x4E9B;&#x53D8;&#x91CF;&#x662F;&#x5B89;&#x88C5;&#x4E0D;&#x540C;&#x7C7B;&#x578B;&#x6587;&#x4EF6;&#x7684;&#x5B50;&#x76EE;&#x5F55;&#x7684;&#x540D;&#x79F0;&#x3002;&#x5728;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x4F7F;&#x7528;&#x4E86;&#x4EE5;&#x4E0B;&#x5185;&#x5BB9;:</p>
<ul>
<li><strong>*CMAKE_INSTALL_BINDIR</strong>&#xFF1A;&#x8FD9;&#x5C06;&#x7528;&#x4E8E;&#x5B9A;&#x4E49;&#x7528;&#x6237;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x6240;&#x5728;&#x7684;&#x5B50;&#x76EE;&#x5F55;&#xFF0C;&#x5373;&#x6240;&#x9009;&#x5B89;&#x88C5;&#x76EE;&#x5F55;&#x4E0B;&#x7684;<code>bin</code>&#x76EE;&#x5F55;&#x3002;</li>
<li><strong>CMAKE_INSTALL_LIBDIR</strong>&#xFF1A;&#x8FD9;&#x5C06;&#x6269;&#x5C55;&#x5230;&#x76EE;&#x6807;&#x4EE3;&#x7801;&#x5E93;(&#x5373;&#x9759;&#x6001;&#x5E93;&#x548C;&#x52A8;&#x6001;&#x5E93;)&#x6240;&#x5728;&#x7684;&#x5B50;&#x76EE;&#x5F55;&#x3002;&#x5728;64&#x4F4D;&#x7CFB;&#x7EDF;&#x4E0A;&#xFF0C;&#x5B83;&#x662F;<code>lib64</code>&#xFF0C;&#x800C;&#x5728;32&#x4F4D;&#x7CFB;&#x7EDF;&#x4E0A;&#xFF0C;&#x5B83;&#x53EA;&#x662F;<code>lib</code>&#x3002;</li>
<li><strong>CMAKE_INSTALL_INCLUDEDIR</strong>&#xFF1A;&#x6700;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x53D8;&#x91CF;&#x4E3A;C&#x5934;&#x6587;&#x4EF6;&#x83B7;&#x53D6;&#x6B63;&#x786E;&#x7684;&#x5B50;&#x76EE;&#x5F55;&#xFF0C;&#x8BE5;&#x53D8;&#x91CF;&#x4E3A;<code>include</code>&#x3002;</li>
</ul>
<p>&#x7136;&#x800C;&#xFF0C;&#x7528;&#x6237;&#x53EF;&#x80FD;&#x5E0C;&#x671B;&#x8986;&#x76D6;&#x8FD9;&#x4E9B;&#x9009;&#x9879;&#x3002;&#x6211;&#x4EEC;&#x5141;&#x8BB8;&#x5728;&#x4E3B;<code>CMakeLists.txt</code>&#x6587;&#x4EF6;&#x4E2D;&#x4F7F;&#x7528;&#x4EE5;&#x4E0B;&#x65B9;&#x5F0F;&#x8986;&#x76D6;&#x9009;&#x9879;:</p>
<pre><code class="lang-cmake"><span class="hljs-comment"># Offer the user the choice</span>
of overriding the installation directories
<span class="hljs-keyword">set</span>(INSTALL_LIBDIR <span class="hljs-variable">${CMAKE_INSTALL_LIBDIR}</span> CACHE PATH
<span class="hljs-string">&quot;Installation directory for libraries&quot;</span>)
<span class="hljs-keyword">set</span>(INSTALL_BINDIR <span class="hljs-variable">${CMAKE_INSTALL_BINDIR}</span> CACHE PATH
<span class="hljs-string">&quot;Installation directory for executables&quot;</span>)
<span class="hljs-keyword">set</span>(INSTALL_INCLUDEDIR <span class="hljs-variable">${CMAKE_INSTALL_INCLUDEDIR}</span> CACHE
PATH <span class="hljs-string">&quot;Installation directory for header files&quot;</span>)
</code></pre>
<p>&#x8FD9;&#x91CD;&#x65B0;&#x5B9A;&#x4E49;&#x4E86;&#x5728;&#x9879;&#x76EE;&#x4E2D;&#x4F7F;&#x7528;&#x7684;<code>INSTALL_BINDIR</code>&#x3001;<code>INSTALL_LIBDIR</code>&#x548C;<code>INSTALL_INCLUDEDIR</code>&#x53D8;&#x91CF;&#x3002;&#x6211;&#x4EEC;&#x8FD8;&#x5B9A;&#x4E49;&#x4E86;<code>INSTALL_CMAKEDIR</code>&#x53D8;&#x91CF;&#xFF0C;&#x4F46;&#x5B83;&#x6240;&#x626E;&#x6F14;&#x7684;&#x89D2;&#x8272;&#x5C06;&#x5728;&#x63A5;&#x4E0B;&#x6765;&#x7684;&#x51E0;&#x4E2A;&#x793A;&#x4F8B;&#x4E2D;&#x8BE6;&#x7EC6;&#x8BA8;&#x8BBA;&#x3002;</p>
<p><strong>TIPS</strong>:<em><code>GNUInstallDirs.cmake</code>&#x6A21;&#x5757;&#x5B9A;&#x4E49;&#x4E86;&#x989D;&#x5916;&#x7684;&#x53D8;&#x91CF;&#xFF0C;&#x8FD9;&#x4E9B;&#x53D8;&#x91CF;&#x5C06;&#x6709;&#x52A9;&#x4E8E;&#xFF0C;&#x5C06;&#x5DF2;&#x5B89;&#x88C5;&#x7684;&#x6587;&#x4EF6;&#x653E;&#x7F6E;&#x5230;&#x6240;&#x9009;&#x5B89;&#x88C5;&#x524D;&#x7F00;&#x7684;&#x5B50;&#x76EE;&#x5F55;&#x4E2D;&#x3002;&#x8BF7;&#x53C2;&#x8003;CMake&#x5728;&#x7EBF;&#x6587;&#x6863;:<a href="https://cmake.org/cmake/help/v3.6/module/GNUInstallDirs.html" target="_blank">https://cmake.org/cmake/help/v3.6/module/GNUInstallDirs.html</a></em></p>
<h3 id="&#x76EE;&#x6807;&#x5C5E;&#x6027;&#x548C;rpath&#x5904;&#x7406;">&#x76EE;&#x6807;&#x5C5E;&#x6027;&#x548C;RPATH&#x5904;&#x7406;</h3>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x66F4;&#x4ED4;&#x7EC6;&#x5730;&#x770B;&#x770B;&#x5728;&#x52A8;&#x6001;&#x5E93;&#x76EE;&#x6807;&#x4E0A;&#x8BBE;&#x7F6E;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x9700;&#x8981;&#x8BBE;&#x7F6E;&#x4EE5;&#x4E0B;&#x5185;&#x5BB9;:</p>
<ul>
<li><code>POSITION_INDEPENDENT_CODE 1</code>&#xFF1A;&#x8BBE;&#x7F6E;&#x751F;&#x6210;&#x4F4D;&#x7F6E;&#x65E0;&#x5173;&#x4EE3;&#x7801;&#x6240;&#x9700;&#x7684;&#x7F16;&#x8BD1;&#x5668;&#x6807;&#x5FD7;&#x3002;&#x6709;&#x5173;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x8003;<a href="https://en.wikipedia.org/wiki/position-independentent_code" target="_blank">https://en.wikipedia.org/wiki/position-independentent_code</a></li>
<li><code>SOVERSION ${PROJECT_VERSION_MAJOR}</code> : &#x8FD9;&#x662F;&#x52A8;&#x6001;&#x5E93;&#x63D0;&#x4F9B;&#x7684;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7F16;&#x7A0B;&#x63A5;&#x53E3;(API)&#x7248;&#x672C;&#x3002;&#x5728;&#x8BBE;&#x7F6E;&#x8BED;&#x4E49;&#x7248;&#x672C;&#x4E4B;&#x540E;&#xFF0C;&#x5C06;&#x5176;&#x8BBE;&#x7F6E;&#x4E3A;&#x4E0E;&#x9879;&#x76EE;&#x7684;&#x4E3B;&#x7248;&#x672C;&#x4E00;&#x81F4;&#x3002;CMake&#x76EE;&#x6807;&#x4E5F;&#x6709;&#x4E00;&#x4E2A;&#x7248;&#x672C;&#x5C5E;&#x6027;&#xFF0C;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x6307;&#x5B9A;&#x76EE;&#x6807;&#x7684;&#x6784;&#x5EFA;&#x7248;&#x672C;&#x3002;&#x6CE8;&#x610F;&#xFF0C;<code>SOVERSION</code>&#x548C;<code>VERSION</code>&#x6709;&#x6240;&#x4E0D;&#x540C;&#xFF1A;&#x968F;&#x7740;&#x65F6;&#x95F4;&#x7684;&#x63A8;&#x79FB;&#xFF0C;&#x63D0;&#x4F9B;&#x76F8;&#x540C;API&#x7684;&#x591A;&#x4E2A;&#x6784;&#x5EFA;&#x7248;&#x672C;&#x3002;&#x672C;&#x4F8B;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x4E0D;&#x5173;&#x5FC3;&#x8FD9;&#x79CD;&#x7684;&#x7C92;&#x5EA6;&#x63A7;&#x5236;&#xFF1A;&#x4EC5;&#x4F7F;&#x7528;<code>SOVERSION</code>&#x5C5E;&#x6027;&#x8BBE;&#x7F6E;API&#x7248;&#x672C;&#x5C31;&#x8DB3;&#x591F;&#x4E86;&#xFF0C;CMake&#x5C06;&#x4E3A;&#x6211;&#x4EEC;&#x5C06;<code>VERSION</code>&#x8BBE;&#x7F6E;&#x4E3A;&#x76F8;&#x540C;&#x7684;&#x503C;&#x3002;&#x76F8;&#x5173;&#x8BE6;&#x7EC6;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x8003;&#x5B98;&#x65B9;&#x6587;&#x6863;:<a href="https://cmake.org/cmake/help/latest/prop_tgt/SOVERSION.html" target="_blank">https://cmake.org/cmake/help/latest/prop_tgt/SOVERSION.html</a></li>
<li><code>OUTPUT_NAME &quot;message&quot;</code>&#xFF1A;&#x8FD9;&#x544A;&#x8BC9;CMake&#x5E93;&#x7684;&#x540D;&#x79F0;<code>message</code>&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x76EE;&#x6807;<code>message-shared</code>&#x7684;&#x540D;&#x79F0;&#xFF0C;<code>libmessage.so.1</code>&#x5C06;&#x5728;&#x6784;&#x5EFA;&#x65F6;&#x751F;&#x6210;&#x3002;&#x4ECE;&#x524D;&#x9762;&#x7ED9;&#x51FA;&#x7684;&#x6784;&#x5EFA;&#x76EE;&#x5F55;&#x548C;&#x5B89;&#x88C5;&#x76EE;&#x5F55;&#x7684;&#x4E5F;&#x53EF;&#x4EE5;&#x770B;&#x51FA;&#xFF0C;<code>libmessage.so</code>&#x7684;&#x7B26;&#x53F7;&#x94FE;&#x63A5;&#x4E5F;&#x5C06;&#x751F;&#x6210;&#x3002;</li>
<li><code>DEBUG_POSTFIX &quot;_d&quot;</code>&#xFF1A;&#x8FD9;&#x544A;&#x8BC9;CMake&#xFF0C;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x4EE5;Debug&#x914D;&#x7F6E;&#x6784;&#x5EFA;&#x9879;&#x76EE;&#xFF0C;&#x5219;&#x5C06;<code>_d</code>&#x540E;&#x7F00;&#x6DFB;&#x52A0;&#x5230;&#x751F;&#x6210;&#x7684;&#x52A8;&#x6001;&#x5E93;&#x3002;</li>
<li><code>PUBLIC_HEADER &quot;Message.hpp&quot;</code>&#xFF1A;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x5C5E;&#x6027;&#x6765;&#x8BBE;&#x7F6E;&#x5934;&#x6587;&#x4EF6;&#x5217;&#x8868;(&#x672C;&#x4F8B;&#x4E2D;&#x53EA;&#x6709;&#x4E00;&#x4E2A;&#x5934;&#x6587;&#x4EF6;)&#xFF0C;&#x58F0;&#x660E;&#x63D0;&#x4F9B;&#x7684;API&#x51FD;&#x6570;&#x3002;&#x8FD9;&#x4E3B;&#x8981;&#x7528;&#x4E8E;macOS&#x4E0A;&#x7684;&#x52A8;&#x6001;&#x5E93;&#x76EE;&#x6807;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x4E8E;&#x5176;&#x4ED6;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x548C;&#x76EE;&#x6807;&#x3002;&#x6709;&#x5173;&#x8BE6;&#x7EC6;&#x4FE1;&#x606F;&#xFF0C;&#x8BF7;&#x53C2;&#x89C1;&#x5B98;&#x65B9;&#x6587;&#x6863;:<a href="https://cmake.org/cmake/help/v3.6/prop_tgt/PUBLIC_HEADER.html" target="_blank">https://cmake.org/cmake/help/v3.6/prop_tgt/PUBLIC_HEADER.html</a></li>
<li><code>MACOSX_RPATH ON</code>&#xFF1A;&#x8FD9;&#x5C06;&#x52A8;&#x6001;&#x5E93;&#x7684;<code>install_name</code>&#x90E8;&#x5206;(&#x76EE;&#x5F55;)&#x8BBE;&#x7F6E;&#x4E3A;macOS&#x4E0A;&#x7684;<code>@rpath</code>&#x3002;</li>
<li><code>WINDOWS_EXPORT_ALL_SYMBOLS ON</code>&#xFF1A;&#x8FD9;&#x5C06;&#x5F3A;&#x5236;&#x5728;Windows&#x4E0A;&#x7F16;&#x8BD1;&#x4EE5;&#x5BFC;&#x51FA;&#x6240;&#x6709;&#x7B26;&#x53F7;&#x3002;&#x6CE8;&#x610F;&#xFF0C;&#x8FD9;&#x901A;&#x5E38;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x597D;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x5728;&#x7B2C;2&#x8282;&#x4E2D;&#x5C55;&#x793A;&#x5982;&#x4F55;&#x751F;&#x6210;&#x5BFC;&#x51FA;&#x5934;&#x6587;&#x4EF6;&#xFF0C;&#x4EE5;&#x53CA;&#x5982;&#x4F55;&#x5728;&#x4E0D;&#x540C;&#x7684;&#x5E73;&#x53F0;&#x4E0A;&#x4FDD;&#x8BC1;&#x7B26;&#x53F7;&#x7684;&#x53EF;&#x89C1;&#x6027;&#x3002;</li>
</ul>
<p>&#x73B0;&#x5728;&#x8BA8;&#x8BBA;&#x4E00;&#x4E0B;<code>RPATH</code>&#x3002;&#x6211;&#x4EEC;&#x5C06;<code>hello-world_wDSO</code>&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x94FE;&#x63A5;&#x5230;<code>libmessage.so.1</code>&#xFF0C;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x5728;&#x6267;&#x884C;&#x65F6;&#xFF0C;&#x5C06;&#x52A0;&#x8F7D;&#x52A8;&#x6001;&#x5E93;&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x6709;&#x5173;&#x5E93;&#x4F4D;&#x7F6E;&#x7684;&#x4FE1;&#x606F;&#x9700;&#x8981;&#x5728;&#x67D0;&#x4E2A;&#x5730;&#x65B9;&#x8FDB;&#x884C;&#x7F16;&#x7801;&#xFF0C;&#x4EE5;&#x4FBF;&#x52A0;&#x8F7D;&#x7A0B;&#x5E8F;&#x80FD;&#x591F;&#x6210;&#x529F;&#x5730;&#x5B8C;&#x6210;&#x5176;&#x5DE5;&#x4F5C;&#x3002;&#x5E93;&#x7684;&#x5B9A;&#x4F4D;&#x6709;&#x4E24;&#x79CD;&#x65B9;&#x6CD5;:</p>
<ul>
<li>&#x901A;&#x8FC7;&#x8BBE;&#x7F6E;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x901A;&#x77E5;&#x94FE;&#x63A5;&#x5668;:<ul>
<li>GNU/Linux&#x4E0A;&#xFF0C;&#x8FD9;&#x9700;&#x8981;&#x5C06;&#x8DEF;&#x5F84;&#x9644;&#x52A0;&#x5230;<code>LD_LIBRARY_PATH</code>&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x4E2D;&#x3002;&#x6CE8;&#x610F;&#xFF0C;&#x8FD9;&#x5F88;&#x53EF;&#x80FD;&#x4F1A;&#x6C61;&#x67D3;&#x7CFB;&#x7EDF;&#x4E2D;&#x6240;&#x6709;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7684;&#x94FE;&#x63A5;&#x5668;&#x8DEF;&#x5F84;&#xFF0C;&#x5E76;&#x53EF;&#x80FD;&#x5BFC;&#x81F4;&#x7B26;&#x53F7;&#x51B2;&#x7A81;( <a href="https://gms.tf/ld_library_path-considered-harmful.htm" target="_blank">https://gms.tf/ld_library_path-considered-harmful.htm</a> )&#x3002;</li>
<li>macOS&#x4E0A;&#xFF0C;&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;<code>DYLD_LIBRARY_PATH</code>&#x53D8;&#x91CF;&#x3002;&#x8FD9;&#x4E0E;GNU/Linux&#x4E0A;&#x7684;<code>LD_LIBRARY_PATH</code>&#x6709;&#x76F8;&#x540C;&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4F7F;&#x7528;<code>DYLD_FALLBACK_LIBRARY_PATH</code>&#x53D8;&#x91CF;&#x6765;(&#x90E8;&#x5206;&#x7684;)&#x6539;&#x5584;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x3002;&#x8BF7;&#x770B;&#x4E0B;&#x9762;&#x7684;&#x94FE;&#x63A5;&#xFF0C;&#x83B7;&#x53D6;&#x76F8;&#x5173;&#x4F8B;&#x5B50;: <a href="https://stackoverflow.com/a/3172515/2528668" target="_blank">https://stackoverflow.com/a/3172515/2528668</a> </li>
</ul>
</li>
<li>&#x53EF;&#x88AB;&#x7F16;&#x7801;&#x5230;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x4E2D;&#xFF0C;&#x4F7F;&#x7528;<code>RPATH</code>&#x53EF;&#x4EE5;&#x8BBE;&#x7F6E;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x7684;&#x8FD0;&#x884C;&#x65F6;&#x641C;&#x7D22;&#x8DEF;&#x5F84;</li>
</ul>
<p>&#x540E;&#x4E00;&#x79CD;&#x65B9;&#x6CD5;&#x66F4;&#x5065;&#x58EE;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x8BBE;&#x7F6E;&#x52A8;&#x6001;&#x5BF9;&#x8C61;&#x7684;<code>RPATH</code>&#x65F6;&#xFF0C;&#x5E94;&#x8BE5;&#x9009;&#x62E9;&#x54EA;&#x4E2A;&#x8DEF;&#x5F84;&#xFF1F;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x786E;&#x4FDD;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x603B;&#x662F;&#x627E;&#x5230;&#x6B63;&#x786E;&#x7684;&#x52A8;&#x6001;&#x5E93;&#xFF0C;&#x4E0D;&#x7BA1;&#x5B83;&#x662F;&#x5728;&#x6784;&#x5EFA;&#x6811;&#x4E2D;&#x8FD0;&#x884C;&#x8FD8;&#x662F;&#x5728;&#x5B89;&#x88C5;&#x6811;&#x4E2D;&#x8FD0;&#x884C;&#x3002;&#x8FD9;&#x9700;&#x8981;&#x901A;&#x8FC7;&#x8BBE;&#x7F6E;<code>hello-world_wDSO</code>&#x76EE;&#x6807;&#x7684;<code>RPATH</code>&#x76F8;&#x5173;&#x5C5E;&#x6027;&#x6765;&#x5B9E;&#x73B0;&#x7684;&#xFF0C;&#x901A;&#x8FC7;<code>$ORIGIN</code>(&#x5728;GNU/Linux&#x4E0A;)&#x6216;<code>@loader_path</code>(&#x5728;macOS&#x4E0A;)&#x53D8;&#x91CF;&#x6765;&#x67E5;&#x627E;&#x4E0E;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x672C;&#x8EAB;&#x4F4D;&#x7F6E;&#x76F8;&#x5173;&#x7684;&#x8DEF;&#x5F84;:</p>
<pre><code class="lang-cmake"><span class="hljs-comment"># Prepare RPATH</span>
file(RELATIVE_PATH _rel <span class="hljs-variable">${CMAKE_INSTALL_PREFIX}</span>/<span class="hljs-variable">${INSTALL_BINDIR}</span> <span class="hljs-variable">${CMAKE_INSTALL_PREFIX}</span>)
<span class="hljs-keyword">if</span>(APPLE)
    <span class="hljs-keyword">set</span>(_rpath <span class="hljs-string">&quot;@loader_path/${_rel}&quot;</span>)
<span class="hljs-keyword">else</span>()
    <span class="hljs-keyword">set</span>(_rpath <span class="hljs-string">&quot;\$ORIGIN/${_rel}&quot;</span>)
<span class="hljs-keyword">endif</span>()
file(TO_NATIVE_PATH <span class="hljs-string">&quot;${_rpath}/${INSTALL_LIBDIR}&quot;</span> message_RPATH)
</code></pre>
<p>&#x5F53;&#x8BBE;&#x7F6E;&#x4E86;<code>message_RPATH</code>&#x53D8;&#x91CF;&#xFF0C;&#x76EE;&#x6807;&#x5C5E;&#x6027;&#x5C06;&#x5B8C;&#x6210;&#x5269;&#x4E0B;&#x7684;&#x5DE5;&#x4F5C;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">set_target_properties</span>(hello-world_wDSO
    PROPERTIES
    MACOSX_RPATH <span class="hljs-keyword">ON</span>
    SKIP_BUILD_RPATH <span class="hljs-keyword">OFF</span>
    BUILD_WITH_INSTALL_RPATH <span class="hljs-keyword">OFF</span>
    INSTALL_RPATH <span class="hljs-string">&quot;${message_RPATH}&quot;</span>
    INSTALL_RPATH_USE_LINK_PATH <span class="hljs-keyword">ON</span>
  )
</code></pre>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x8BE6;&#x7EC6;&#x7814;&#x7A76;&#x4E00;&#x4E0B;&#x8FD9;&#x4E2A;&#x547D;&#x4EE4;&#xFF1A;</p>
<ul>
<li><code>SKIP_BUILD_RPATH OFF</code>&#xFF1A;&#x544A;&#x8BC9;CMake&#x751F;&#x6210;&#x9002;&#x5F53;&#x7684;<code>RPATH</code>&#xFF0C;&#x4EE5;&#x4FBF;&#x80FD;&#x591F;&#x5728;&#x6784;&#x5EFA;&#x6811;&#x4E2D;&#x8FD0;&#x884C;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x3002;</li>
<li><code>UILD_WITH_INSTALL_RPATH OFF</code>&#xFF1A;&#x5173;&#x95ED;&#x751F;&#x6210;&#x53EF;&#x6267;&#x884C;&#x76EE;&#x6807;&#xFF0C;&#x4F7F;&#x5176;<code>RPATH</code>&#x8C03;&#x6574;&#x4E3A;&#x4E0E;&#x5B89;&#x88C5;&#x6811;&#x7684;<code>RPATH</code>&#x76F8;&#x540C;&#x3002;&#x5728;&#x6784;&#x5EFA;&#x6811;&#x4E2D;&#x4E0D;&#x8FD0;&#x884C;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x3002;</li>
<li><code>INSTALL_RPATH &quot;${message_RPATH}&quot;</code>&#xFF1A;&#x5C06;&#x5DF2;&#x5B89;&#x88C5;&#x7684;&#x53EF;&#x6267;&#x884C;&#x76EE;&#x6807;&#x7684;<code>RPATH</code>&#x8BBE;&#x7F6E;&#x4E3A;&#x5148;&#x524D;&#x7684;&#x8DEF;&#x5F84;&#x3002;</li>
<li><code>INSTALL_RPATH_USE_LINK_PATH ON</code>&#xFF1A;&#x544A;&#x8BC9;CMake&#x5C06;&#x94FE;&#x63A5;&#x5668;&#x641C;&#x7D22;&#x8DEF;&#x5F84;&#x9644;&#x52A0;&#x5230;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x7684;<code>RPATH</code>&#x4E2D;&#x3002;</li>
</ul>
<p><strong>NOTE</strong>:<em>&#x52A0;&#x8F7D;&#x5668;&#x5728;Unix&#x7CFB;&#x7EDF;&#x4E0A;&#x5982;&#x4F55;&#x5DE5;&#x4F5C;&#x7684;&#x66F4;&#x591A;&#x4FE1;&#x606F;&#xFF0C;&#x53EF;&#x53C2;&#x89C1;:<a href="http://longwei.github.io/rpath_origin/" target="_blank">http://longwei.github.io/rpath_origin/</a></em></p>
<h3 id="&#x5B89;&#x88C5;&#x6307;&#x4EE4;">&#x5B89;&#x88C5;&#x6307;&#x4EE4;</h3>
<p>&#x6700;&#x540E;&#xFF0C;&#x770B;&#x4E00;&#x4E0B;&#x5B89;&#x88C5;&#x6307;&#x4EE4;&#x3002;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5B89;&#x88C5;&#x4E00;&#x4E2A;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x3001;&#x4E00;&#x4E2A;&#x5E93;&#x548C;&#x4E00;&#x4E2A;&#x5934;&#x6587;&#x4EF6;&#x3002;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x548C;&#x5E93;&#x662F;&#x6784;&#x5EFA;&#x76EE;&#x6807;&#xFF0C;&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x5B89;&#x88C5;&#x547D;&#x4EE4;&#x7684;<code>TARGETS</code>&#x9009;&#x9879;&#x3002;&#x53EF;&#x4EE5;&#x540C;&#x65F6;&#x8BBE;&#x7F6E;&#x591A;&#x4E2A;&#x76EE;&#x6807;&#x7684;&#x5B89;&#x88C5;&#x89C4;&#x5219;&#xFF1A;CMake&#x77E5;&#x9053;&#x5B83;&#x4EEC;&#x662F;&#x4EC0;&#x4E48;&#x7C7B;&#x578B;&#x7684;&#x76EE;&#x6807;&#xFF0C;&#x65E0;&#x8BBA;&#x5176;&#x662F;&#x53EF;&#x6267;&#x884C;&#x7A0B;&#x5E8F;&#x5E93;&#x3001;&#x52A8;&#x6001;&#x5E93;&#xFF0C;&#x8FD8;&#x662F;&#x9759;&#x6001;&#x5E93;:</p>
<pre><code class="lang-cmake"><span class="hljs-keyword">install</span>(
  TARGETS
    <span class="hljs-keyword">message</span>-shared
    hello-world_wDSO
</code></pre>
<p>&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x5C06;&#x5B89;&#x88C5;&#x5728;<code>RUNTIME DESTINATION</code>&#xFF0C;&#x5C06;&#x5176;&#x8BBE;&#x7F6E;&#x4E3A;<code>${INSTALL_BINDIR}</code>&#x3002;&#x52A8;&#x6001;&#x5E93;&#x5B89;&#x88C5;&#x5230;<code>LIBRARY_DESTINATION</code>&#xFF0C;&#x5C06;&#x5176;&#x8BBE;&#x7F6E;&#x4E3A;<code>${INSTALL_LIBDIR}</code>&#x3002;&#x9759;&#x6001;&#x5E93;&#x5C06;&#x5B89;&#x88C5;&#x5230;<code>ARCHIVE DESTINATION</code>&#xFF0C;&#x5C06;&#x5176;&#x8BBE;&#x7F6E;&#x4E3A;<code>${INSTALL_LIBDIR}</code>:</p>
<pre><code class="lang-cmake">ARCHIVE
  DESTINATION <span class="hljs-variable">${INSTALL_LIBDIR}</span>
  COMPONENT lib
RUNTIME
  DESTINATION <span class="hljs-variable">${INSTALL_BINDIR}</span>
  COMPONENT bin
LIBRARY
  DESTINATION <span class="hljs-variable">${INSTALL_LIBDIR}</span>
  COMPONENT lib
</code></pre>
<p>&#x6CE8;&#x610F;&#xFF0C;&#x8FD9;&#x91CC;&#x4E0D;&#x4EC5;&#x6307;&#x5B9A;&#x4E86;<code>DESTINATION</code>&#xFF0C;&#x8FD8;&#x6307;&#x5B9A;&#x4E86;<code>COMPONENT</code>&#x3002;&#x4F7F;&#x7528;<code>cmake --build . --target install</code>&#x5B89;&#x88C5;&#x547D;&#x4EE4;&#xFF0C;&#x6240;&#x6709;&#x7EC4;&#x4EF6;&#x4F1A;&#x6309;&#x9884;&#x671F;&#x5B89;&#x88C5;&#x5B8C;&#x6BD5;&#x3002;&#x7136;&#x800C;&#xFF0C;&#x6709;&#x65F6;&#x53EA;&#x5B89;&#x88C5;&#x5176;&#x4E2D;&#x4E00;&#x4E9B;&#x53EF;&#x7528;&#x7684;&#x3002;&#x8FD9;&#x5C31;&#x662F;<code>COMPONENT</code>&#x5173;&#x952E;&#x5B57;&#x5E2E;&#x52A9;&#x6211;&#x4EEC;&#x505A;&#x7684;&#x4E8B;&#x60C5;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5F53;&#x53EA;&#x8981;&#x6C42;&#x5B89;&#x88C5;&#x5E93;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6267;&#x884C;&#x4EE5;&#x4E0B;&#x6B65;&#x9AA4;:</p>
<pre><code class="lang-shell">$ cmake -D COMPONENT=lib -P cmake_install.cmake
</code></pre>
<p>&#x81EA;&#x4ECE;<code>Message.hpp</code>&#x5934;&#x6587;&#x4EF6;&#x8BBE;&#x7F6E;&#x4E3A;&#x9879;&#x76EE;&#x7684;&#x516C;&#x5171;&#x5934;&#x6587;&#x4EF6;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>PUBLIC_HEADER</code>&#x5173;&#x952E;&#x5B57;&#x5C06;&#x5176;&#x4E0E;&#x5176;&#x4ED6;&#x76EE;&#x6807;&#x5B89;&#x88C5;&#x5230;&#x9009;&#x62E9;&#x7684;&#x76EE;&#x7684;&#x5730;:<code>${INSTALL_INCLUDEDIR}/message</code>&#x3002;&#x5E93;&#x7528;&#x6237;&#x73B0;&#x5728;&#x53EF;&#x4EE5;&#x5305;&#x542B;&#x5934;&#x6587;&#x4EF6;&#xFF1A;<code>#include &lt;message/Message.hpp&gt;</code>&#xFF0C;&#x8FD9;&#x9700;&#x8981;&#x5728;&#x7F16;&#x8BD1;&#x65F6;&#xFF0C;&#x4F7F;&#x7528;<code>-I</code>&#x9009;&#x9879;&#x5C06;&#x6B63;&#x786E;&#x7684;&#x5934;&#x6587;&#x4EF6;&#x67E5;&#x627E;&#x8DEF;&#x5F84;&#x4F4D;&#x7F6E;&#x4F20;&#x9012;&#x7ED9;&#x7F16;&#x8BD1;&#x5668;&#x3002;</p>
<p>&#x5B89;&#x88C5;&#x6307;&#x4EE4;&#x4E2D;&#x7684;&#x5404;&#x79CD;&#x76EE;&#x6807;&#x5730;&#x5740;&#x4F1A;&#x88AB;&#x89E3;&#x91CA;&#x4E3A;&#x76F8;&#x5BF9;&#x8DEF;&#x5F84;&#xFF0C;&#x9664;&#x975E;&#x4F7F;&#x7528;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;&#x3002;&#x4F46;&#x662F;&#x76F8;&#x5BF9;&#x4E8E;&#x54EA;&#x91CC;&#x5462;&#xFF1F;&#x6839;&#x636E;&#x4E0D;&#x540C;&#x7684;&#x5B89;&#x88C5;&#x5DE5;&#x5177;&#x800C;&#x4E0D;&#x540C;&#xFF0C;&#x800C;CMake&#x53EF;&#x4EE5;&#x53BB;&#x8BA1;&#x7B97;&#x76EE;&#x6807;&#x5730;&#x5740;&#x7684;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;&#x3002;&#x5F53;&#x4F7F;&#x7528;<code>cmake --build . --target install</code>&#xFF0C;&#x8DEF;&#x5F84;&#x5C06;&#x76F8;&#x5BF9;&#x4E8E;<code>CMAKE_INSTALL_PREFIX</code>&#x8BA1;&#x7B97;&#x3002;&#x4F46;&#x5F53;&#x4F7F;&#x7528;CPack&#x65F6;&#xFF0C;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;&#x5C06;&#x76F8;&#x5BF9;&#x4E8E;<code>CPACK_PACKAGING_INSTALL_PREFIX</code>&#x8BA1;&#x7B97;&#x3002;CPack&#x7684;&#x7528;&#x6CD5;&#x5C06;&#x5728;&#x7B2C;11&#x7AE0;&#x4E2D;&#x4ECB;&#x7ECD;&#x3002;</p>
<p><strong>NOTE</strong>:<em>Unix Makefile&#x548C;Ninja&#x751F;&#x6210;&#x5668;&#x8FD8;&#x63D0;&#x4F9B;&#x4E86;&#x53E6;&#x4E00;&#x79CD;&#x673A;&#x5236;:<code>DESTDIR</code>&#x3002;&#x53EF;&#x4EE5;&#x5728;<code>DESTDIR</code>&#x6307;&#x5B9A;&#x7684;&#x76EE;&#x5F55;&#x4E0B;&#x91CD;&#x65B0;&#x5B9A;&#x4F4D;&#x6574;&#x4E2A;&#x5B89;&#x88C5;&#x6811;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;<code>env DESTDIR=/tmp/stage cmake --build . --target install</code>&#x5C06;&#x5B89;&#x88C5;&#x76F8;&#x5BF9;&#x4E8E;<code>CMAKE_INSTALL_PREFIX</code>&#x548C;<code>/tmp/stage</code>&#x76EE;&#x5F55;&#x3002;&#x53EF;&#x4EE5;&#x5728;&#x8FD9;&#x91CC;&#x9605;&#x8BFB;&#x66F4;&#x591A;&#x4FE1;&#x606F;:<a href="https://www.gnu.org/prep/standards/html_node/DESTDIR.html" target="_blank">https://www.gnu.org/prep/standards/html_node/DESTDIR.html</a></em></p>
<h2 id="&#x66F4;&#x591A;&#x4FE1;&#x606F;">&#x66F4;&#x591A;&#x4FE1;&#x606F;</h2>
<p>&#x6B63;&#x786E;&#x8BBE;&#x7F6E;<code>RPATH</code>&#x53EF;&#x80FD;&#x76F8;&#x5F53;&#x9EBB;&#x70E6;&#xFF0C;&#x4F46;&#x8FD9;&#x5BF9;&#x4E8E;&#x7528;&#x6237;&#x6765;&#x8BF4;&#x65E0;&#x6CD5;&#x907F;&#x514D;&#x3002;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;CMake&#x8BBE;&#x7F6E;&#x53EF;&#x6267;&#x884C;&#x7A0B;&#x5E8F;&#x7684;<code>RPATH</code>&#xFF0C;&#x5047;&#x8BBE;&#x5B83;&#x4EEC;&#x5C06;&#x4ECE;&#x6784;&#x5EFA;&#x6811;&#x8FD0;&#x884C;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x5B89;&#x88C5;&#x4E4B;&#x540E;<code>RPATH</code>&#x88AB;&#x6E05;&#x9664;&#xFF0C;&#x5F53;&#x7528;&#x6237;&#x60F3;&#x8981;&#x8FD0;&#x884C;<code>hello-world_wDSO</code>&#x65F6;&#xFF0C;&#x5C31;&#x4F1A;&#x51FA;&#x73B0;&#x95EE;&#x9898;&#x3002;&#x4F7F;&#x7528;Linux&#x4E0A;&#x7684;<code>ldd</code>&#x5DE5;&#x5177;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x68C0;&#x67E5;&#x6784;&#x5EFA;&#x6811;&#x4E2D;&#x7684;<code>hello-world_wDSO</code>&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#xFF0C;&#x8FD0;&#x884C;<code>ldd hello-world_wDSO</code>&#x5C06;&#x5F97;&#x5230;&#x4EE5;&#x4E0B;&#x7ED3;&#x679C;:</p>
<pre><code class="lang-shell">libmessage.so.1 =&gt; /home/user/cmake-cookbook/chapter-10/recipe-01/cxx-example/build/lib64/libmessage.so.1(0x00007f7a92e44000)
</code></pre>
<p>&#x5728;&#x5B89;&#x88C5;&#x76EE;&#x5F55;&#x4E2D;&#x8FD0;&#x884C;<code>ldd hello-world_wDSO</code>&#x5C06;&#x5F97;&#x5230;&#x4EE5;&#x4E0B;&#x7ED3;&#x679C;:</p>
<pre><code class="lang-shell">libmessage.so.1 =&gt; Not found
</code></pre>
<p>&#x8FD9;&#x663E;&#x7136;&#x662F;&#x4E0D;&#x884C;&#x7684;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x603B;&#x662F;&#x786C;&#x7F16;&#x7801;<code>RPATH</code>&#x6765;&#x6307;&#x5411;&#x6784;&#x5EFA;&#x6811;&#x6216;&#x5B89;&#x88C5;&#x76EE;&#x5F55;&#x4E5F;&#x662F;&#x9519;&#x8BEF;&#x7684;&#xFF1A;&#x8FD9;&#x4E24;&#x4E2A;&#x4F4D;&#x7F6E;&#x4E2D;&#x7684;&#x4EFB;&#x4F55;&#x4E00;&#x4E2A;&#x90FD;&#x53EF;&#x80FD;&#x88AB;&#x5220;&#x9664;&#xFF0C;&#x4ECE;&#x800C;&#x5BFC;&#x81F4;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x7684;&#x635F;&#x574F;&#x3002;&#x8FD9;&#x91CC;&#x7ED9;&#x51FA;&#x7684;&#x89E3;&#x51B3;&#x65B9;&#x6848;&#x4E3A;&#x6784;&#x5EFA;&#x6811;&#x548C;&#x5B89;&#x88C5;&#x76EE;&#x5F55;&#x4E2D;&#x7684;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x8BBE;&#x7F6E;&#x4E86;&#x4E0D;&#x540C;&#x7684;<code>RPATH</code>&#xFF0C;&#x56E0;&#x6B64;&#x5B83;&#x603B;&#x662F;&#x6307;&#x5411;&#x201C;&#x6709;&#x610F;&#x4E49;&#x201D;&#x7684;&#x4F4D;&#x7F6E;&#xFF1B;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x5C3D;&#x53EF;&#x80FD;&#x63A5;&#x8FD1;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x3002;&#x5728;&#x6784;&#x5EFA;&#x6811;&#x4E2D;&#x8FD0;&#x884C;<code>ldd</code>&#x663E;&#x793A;&#x76F8;&#x540C;&#x7684;&#x8F93;&#x51FA;:</p>
<pre><code class="lang-shell">libmessage.so.1 =&gt; /home/roberto/Workspace/robertodr/cmake-
cookbook/chapter-10/recipe-01/cxx-example/build/lib64/libmessage.so.1
(0x00007f7a92e44000)
</code></pre>
<p>&#x53E6;&#x5916;&#xFF0C;&#x5728;&#x5B89;&#x88C5;&#x76EE;&#x5F55;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x5F97;&#x5230;:</p>
<pre><code class="lang-shell">libmessage.so.1 =&gt; /home/roberto/Software/ch10r01/bin/../lib64/libmessage.so.1 (0x00007fbd2a725000)
</code></pre>
<p>&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x4E86;&#x5E26;&#x6709;&#x76EE;&#x6807;&#x53C2;&#x6570;&#x7684;CMake&#x5B89;&#x88C5;&#x547D;&#x4EE4;&#xFF0C;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5B89;&#x88C5;&#x6784;&#x5EFA;&#x76EE;&#x6807;&#x3002;&#x800C;&#x8BE5;&#x547D;&#x4EE4;&#x8FD8;&#x6709;&#x53E6;&#x5916;4&#x4E2A;&#x53C2;&#x6570;:</p>
<ul>
<li><strong>FILES</strong>&#x548C;<strong>PROGRAMS</strong>&#xFF0C;&#x5206;&#x522B;&#x7528;&#x4E8E;&#x5B89;&#x88C5;&#x6587;&#x4EF6;&#x6216;&#x7A0B;&#x5E8F;&#x3002;&#x5B89;&#x88C5;&#x540E;&#xFF0C;&#x5E76;&#x8BBE;&#x7F6E;&#x5B89;&#x88C5;&#x6587;&#x4EF6;&#x9002;&#x5F53;&#x7684;&#x6743;&#x9650;&#x3002;&#x5BF9;&#x4E8E;&#x6587;&#x4EF6;&#xFF0C;&#x5BF9;&#x6240;&#x6709;&#x8005;&#x5177;&#x6709;&#x8BFB;&#x548C;&#x5199;&#x6743;&#x9650;&#xFF0C;&#x5BF9;&#x7EC4;&#x4EE5;&#x53CA;&#x5176;&#x4ED6;&#x7528;&#x6237;&#x548C;&#x7EC4;&#x5177;&#x6709;&#x8BFB;&#x6743;&#x9650;&#x3002;&#x5BF9;&#x4E8E;&#x7A0B;&#x5E8F;&#xFF0C;&#x5C06;&#x6388;&#x4E88;&#x6267;&#x884C;&#x6743;&#x9650;&#x3002;&#x6CE8;&#x610F;&#xFF0C;<code>PROGRAMS</code>&#x8981;&#x4E0E;&#x975E;&#x6784;&#x5EFA;&#x76EE;&#x6807;&#x7684;&#x53EF;&#x6267;&#x884C;&#x7A0B;&#x5E8F;&#x4E00;&#x8D77;&#x4F7F;&#x7528;&#x3002;&#x53C2;&#x89C1;: <a href="https://cmake.org/cmake/help/v3.6/command/install.html#installing-files" target="_blank">https://cmake.org/cmake/help/v3.6/command/install.html#installing-files</a></li>
<li><strong>DIRECTORY</strong>&#xFF0C;&#x7528;&#x4E8E;&#x5B89;&#x88C5;&#x76EE;&#x5F55;&#x3002;&#x5F53;&#x53EA;&#x7ED9;&#x51FA;&#x4E00;&#x4E2A;&#x76EE;&#x5F55;&#x540D;&#x65F6;&#xFF0C;&#x5B83;&#x901A;&#x5E38;&#x88AB;&#x7406;&#x89E3;&#x4E3A;&#x76F8;&#x5BF9;&#x4E8E;&#x5F53;&#x524D;&#x6E90;&#x76EE;&#x5F55;&#x3002;&#x53EF;&#x4EE5;&#x5BF9;&#x76EE;&#x5F55;&#x7684;&#x5B89;&#x88C5;&#x7C92;&#x5EA6;&#x8FDB;&#x884C;&#x63A7;&#x5236;&#x3002;&#x8BF7;&#x53C2;&#x8003;&#x5728;&#x7EBF;&#x6587;&#x6863;&#xFF1A; <a href="https://cmake.org/cmake/help/v3.6/command/install.html#installing-directories" target="_blank">https://cmake.org/cmake/help/v3.6/command/install.html#installing-directories</a></li>
<li><strong>SCRIPT</strong>&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5B83;&#x5728;CMake&#x811A;&#x672C;&#x4E2D;&#x5B9A;&#x4E49;&#x81EA;&#x5B9A;&#x4E49;&#x5B89;&#x88C5;&#x89C4;&#x5219;&#x3002;&#x53C2;&#x89C1;&#xFF1A; <a href="https://cmake.org/cmake/help/v3.6/command/install.html#custom-installation-logic" target="_blank">https://cmake.org/cmake/help/v3.6/command/install.html#custom-installation-logic</a></li>
<li><strong>EXPORT</strong>&#xFF0C;&#x6211;&#x4EEC;&#x5C06;&#x6B64;&#x53C2;&#x6570;&#x7684;&#x8BA8;&#x8BBA;&#x63A8;&#x8FDF;&#x5230;&#x7B2C;3&#x8282;&#xFF0C;&#x8BE5;&#x53C2;&#x6570;&#x7528;&#x4E8E;&#x5BFC;&#x51FA;&#x76EE;&#x6807;&#x3002;</li>
</ul>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="10.0-chinese.html" class="navigation navigation-prev " aria-label="Previous page: 第10章 编写安装程序">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="10.2-chinese.html" class="navigation navigation-next " aria-label="Next page: 10.2 生成输出头文件">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"10.1 安装项目","level":"1.13.1","depth":2,"next":{"title":"10.2 生成输出头文件","level":"1.13.2","depth":2,"path":"content/chapter10/10.2-chinese.md","ref":"content/chapter10/10.2-chinese.md","articles":[]},"previous":{"title":"第10章 编写安装程序","level":"1.13","depth":1,"path":"content/chapter10/10.0-chinese.md","ref":"content/chapter10/10.0-chinese.md","articles":[{"title":"10.1 安装项目","level":"1.13.1","depth":2,"path":"content/chapter10/10.1-chinese.md","ref":"content/chapter10/10.1-chinese.md","articles":[]},{"title":"10.2 生成输出头文件","level":"1.13.2","depth":2,"path":"content/chapter10/10.2-chinese.md","ref":"content/chapter10/10.2-chinese.md","articles":[]},{"title":"10.3 输出目标","level":"1.13.3","depth":2,"path":"content/chapter10/10.3-chinese.md","ref":"content/chapter10/10.3-chinese.md","articles":[]},{"title":"10.4 安装超级构建","level":"1.13.4","depth":2,"path":"content/chapter10/10.4-chinese.md","ref":"content/chapter10/10.4-chinese.md","articles":[]}]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","search-pro","-sharing","github","pageview-count"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"github":{"url":"https://github.com/arcticfox1919"},"search-pro":{},"pageview-count":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"陈晓伟 chenxiaowei","pdf":{"pageNumbers":false,"fontSize":30,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":30,"left":30,"top":30,"bottom":50}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"《CMake Cookbook》中文版","language":"zh-hans","gitbook":"*","description":"作为对《CMake Cookbook》英文版的中文翻译。","direction":"ltr"},"file":{"path":"content/chapter10/10.1-chinese.md","mtime":"2020-09-22T10:39:01.890Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-09-22T11:21:21.825Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

